如何使用simple_form预选关联复选框

时间:2022-09-03 15:57:22

I have this piece of code, while using simple_form:

我有这段代码,同时使用simple_form:

= simple_form_for :report do |f|
  = f.association :presets,
    :collection => @account.presets.collect{ |p| [p.name, p.id] },
    :as => :check_boxes

How can I preselect a specific preset checkbox, knowing that the ID of this preset is passed within params[:preset_id]? The checkboxes' HTML name attributes are report[preset_ids][].

如何知道此预设的ID是否在params [:preset_id]内传递,我该如何预先选择一个特定的预设复选框?复选框的HTML名称属性为report [preset_ids] []。

4 个解决方案

#1


29  

According to the simple_form documentation:

根据simple_form文档:

The association helper just invokes input under the hood, so all options available to :select, :radio and :check_boxes are also available to association. Additionally, you can specify the collection by hand, all together with the prompt:

关联助手只是调用引擎下的输入,因此所有可用的选项:select,:radio和:check_boxes也可用于关联。此外,您可以手动指定集合,以及提示:

   f.association :company, :collection
      => Company.active.all(:order => 'name'), :prompt => "Choose a Company"

So, you should use something like this:

所以,你应该使用这样的东西:

= simple_form_for :report do |f|
  = f.association :presets,
    :collection => @account.presets.collect{ |p| [p.name, p.id] },
    :as => :check_boxes,
    :checked => params[:preset_id]

I don't have experience with simple_form, but this might help :)

我没有使用simple_form的经验,但这可能有帮助:)

#2


25  

An update for everybody. the :selected option did not work for me. I used:

每个人的更新。 :选择的选项对我不起作用。我用了:

:checked => [2, 3]

Hope it helps someone.

希望它可以帮到某人。

#3


1  

f.association Really did the trick, thanks :), for preselecting, saving, and everything, I don't have reputation enough to vote up your answer (@claudio-acciaresi) that's why I'm commenting here...

f.association真的做了诀窍,谢谢:),预选,保存,以及一切,我没有足够的声誉来表达你的答案(@ claudio-acciaresi)这就是我在这里评论的原因......

This is my snippet:

这是我的片段:

<%= f.association :association, collection: Model.all, 
      value_method: :id, label_method: :name, 
      as: :check_boxes, include_blank: false %>

Replace symbol :association with the current has_many from the model. Replace Model.all, for your source data.

替换符号:与模型中当前的has_many关联。替换Model.all,以获取源数据。

Hope it gets useful for someone else :)

希望它对其他人有用:)

Regards.

问候。

#4


0  

Don't forget to cast params[:preset_id] to integer:

不要忘记将params [:preset_id]强制转换为整数:

params[:preset_id].to_i

#1


29  

According to the simple_form documentation:

根据simple_form文档:

The association helper just invokes input under the hood, so all options available to :select, :radio and :check_boxes are also available to association. Additionally, you can specify the collection by hand, all together with the prompt:

关联助手只是调用引擎下的输入,因此所有可用的选项:select,:radio和:check_boxes也可用于关联。此外,您可以手动指定集合,以及提示:

   f.association :company, :collection
      => Company.active.all(:order => 'name'), :prompt => "Choose a Company"

So, you should use something like this:

所以,你应该使用这样的东西:

= simple_form_for :report do |f|
  = f.association :presets,
    :collection => @account.presets.collect{ |p| [p.name, p.id] },
    :as => :check_boxes,
    :checked => params[:preset_id]

I don't have experience with simple_form, but this might help :)

我没有使用simple_form的经验,但这可能有帮助:)

#2


25  

An update for everybody. the :selected option did not work for me. I used:

每个人的更新。 :选择的选项对我不起作用。我用了:

:checked => [2, 3]

Hope it helps someone.

希望它可以帮到某人。

#3


1  

f.association Really did the trick, thanks :), for preselecting, saving, and everything, I don't have reputation enough to vote up your answer (@claudio-acciaresi) that's why I'm commenting here...

f.association真的做了诀窍,谢谢:),预选,保存,以及一切,我没有足够的声誉来表达你的答案(@ claudio-acciaresi)这就是我在这里评论的原因......

This is my snippet:

这是我的片段:

<%= f.association :association, collection: Model.all, 
      value_method: :id, label_method: :name, 
      as: :check_boxes, include_blank: false %>

Replace symbol :association with the current has_many from the model. Replace Model.all, for your source data.

替换符号:与模型中当前的has_many关联。替换Model.all,以获取源数据。

Hope it gets useful for someone else :)

希望它对其他人有用:)

Regards.

问候。

#4


0  

Don't forget to cast params[:preset_id] to integer:

不要忘记将params [:preset_id]强制转换为整数:

params[:preset_id].to_i