找到你要的答案

Q:How to select options from array with form_tag and checkboxes

Q:如何选择与form_tag和复选框阵列选项

I have an array @sources that contains names (strings) of sources. In a view I want to list every source with a checkbox in a remote form. Then in the linked controller action I would like to have an array in the params hash, containing only those sources, that the user has checked before submitting.

I tried doing that manually, like so:

<%= form_tag select_sources_user_wordsearch_path(@user, @wordsearch), {method: :post, remote: true} do %>
  <% @sources.each do |source| %>
    <div class="form-group">
      <div class="checkbox">
        <label class="pull-left">
          <input type="checkbox" class="pull-right"> <%=source%>
        </label>
      </div>
    </div>
  <% end %>
  <div class="form-group">
    <%= submit_tag "Start Search",id:"search_commit", class: "btn btn-primary" %>
  </div>
<% end %>

But when submitting the form, I don't even see a params hash:

Started POST "/users/1/wordsearches/77/select_sources" for 127.0.0.1 at 2015-08-15 16:18:06 +0200
Processing by WordsearchesController#select_sources as JS
Parameters: {"utf8"=>"✓", "commit"=>"Start Search", "user_id"=>"1", "id"=>"77"}

So I was looking around to see what's going on with the form_tag helper and check_box_tag. And in check_box_tag's API page (see comments) I found that it is possible to pass a collection into it and receive the result of the selection in an array in the params hash. Basically, what I described above.

I am wondering how this would be done inside a form_tag for a remote form, with an array (instead of a collection)?

In the form_tag API page I couldn't find anything about how the data is transferred to the controller. In the check_box_tag there is nothing mentioned about how to handle collections.

Can anyone tell me how this is done "the Rails way"? It has to be easier than I think..

我有一个数组,包含源的名称(字符串)。在我想在一个偏远的形式一个复选框,每个源列表。然后在联动控制器动作我想在params hash数组只包含那些来源,用户在提交之前检查。

我试着手动做,像这样:

<%= form_tag select_sources_user_wordsearch_path(@user, @wordsearch), {method: :post, remote: true} do %>
  <% @sources.each do |source| %>
    <div class="form-group">
      <div class="checkbox">
        <label class="pull-left">
          <input type="checkbox" class="pull-right"> <%=source%>
        </label>
      </div>
    </div>
  <% end %>
  <div class="form-group">
    <%= submit_tag "Start Search",id:"search_commit", class: "btn btn-primary" %>
  </div>
<% end %>

但当提交表单,我甚至看不到一个参数hash:

Started POST "/users/1/wordsearches/77/select_sources" for 127.0.0.1 at 2015-08-15 16:18:06 +0200
Processing by WordsearchesController#select_sources as JS
Parameters: {"utf8"=>"✓", "commit"=>"Start Search", "user_id"=>"1", "id"=>"77"}

让我看看发生了什么form_tag帮手和check_box_tag。在check_box_tag API页面(见注释)我发现可以通过收集到并接受选择的结果在参数hash数组。基本上,我上面所描述的。

我想知道怎样才能做到这一form_tag里面一个远程的形式,用数组(而不是一个集合)?

在form_tag API的页面,我找不到任何关于数据如何被传送到控制器。在check_box_tag没有提到如何处理收藏。

Can anyone tell me how this is done "the Rails way"? It has to be easier than I think..

answer1: 回答1:

First question: why don't you use the the form_for helper? Could be something like form_for @wordsearch, url: select_sources_user_wordsearch_path(@user, @wordsearch), {method: :post, remote: true} do.

You may also have a look at the collection_check_boxes helper in the API.

Afaik this can only be used with form_for and the corresponding form object.

<%= form_for @wordsearch do |form| %>
  <%= form.collection_check_boxes(:source_ids, @sources, :id, :labeling_method ) %>
<% end %>

Assuming @sources is some kind of an object. In your case

<%= form_for @wordsearch do |form| %>
  <%= form.collection_check_boxes(:sources, @sources, :to_s, :to_s ) %>
<% end %>

could work as well. (Did not try it!)

Another way would be to just send the values manually like so:

<% @sources.each do |source| -%>
   <%= check_box_tag "wordsearch[sources][]", source -%>
   <%= source -%>
<% end -%>

Hope this helps.

第一个问题:你为什么不使用的form_for帮手吗?可以像form_for @单词搜索,网址:select_sources_user_wordsearch_path(@用户@ wordsearch),{方法:后,远程:true}做。

你也可能在API的collection_check_boxes帮手一看。

就我所知这只能用form_for和相应的表单对象。

<%= form_for @wordsearch do |form| %>
  <%= form.collection_check_boxes(:source_ids, @sources, :id, :labeling_method ) %>
<% end %>

假设源是某种对象。在你的情况

<%= form_for @wordsearch do |form| %>
  <%= form.collection_check_boxes(:sources, @sources, :to_s, :to_s ) %>
<% end %>

也能工作。(没试过!)

另一种方法是只发送值手动这样:

<% @sources.each do |source| -%>
   <%= check_box_tag "wordsearch[sources][]", source -%>
   <%= source -%>
<% end -%>

希望这有助于。

arrays  ruby-on-rails-4