简体   繁体   English

如何在form_tag块中创建自定义哈希

[英]How to create a custom hash inside form_tag block

I've got a Rails 3 app and I'm trying to create multiple db records from aa single form. 我有一个Rails 3应用程序,我试图从一个表单创建多个数据库记录。

I've got a model, contents, which as attributes of body and id. 我有一个模型,内容,它是body和id的属性。 I've got a view that displays a form that lists all records and puts a checkbox next to it (and sets the value to true/checked). 我有一个视图,该视图显示一个列出所有记录的表单,并在其旁边放置一个复选框(并将值设置为true / checked)。 A user has the option to un-select content records that they don't want. 用户可以选择取消选择他们不想要的内容记录。

<%= form_tag 'contents' do %>
  <% @contents.each do |content| %>
    <%= label_tag content.body %>
    <%= check_box_tag content.id, content.body, true %>
  <% end %>
  <br />
  <%= submit_tag "Next" %>
<% end %>

When this form pots, I get params that looks like this: 当这种形式的锅,我得到的参数看起来像这样:

{"utf8"=>"✓",
 "authenticity_token"=>cooltoken",
 "1"=>"asdfasfdf",
 "2"=>"asdfasdf adf",
 "3"=>"asdf asdf asdf",
 "commit"=>"Next",
 "action"=>"create",
"controller"=>"contents"}

But what I want is a params hash that looks like this: 但是我想要的是一个参数哈希,看起来像这样:

{"utf8"=>"✓",
 "authenticity_token"=>"cooltoken",
 'user_selections' => {
                      "1"=>"asdfasfdf",
                      "2"=>"asdfasdf adf",
                      "3"=>"asdf asdf asdf",
                     } 
"commit"=>"Next",
"action"=>"create",
"controller"=>"contents"}

How do I push the contents info into it's own hash (that I'm calling the 'user_selection' hash above)? 如何将内容信息推入自己的哈希(我在上面称为“ user_selection”哈希)?

尝试这个:

<%= check_box_tag content.id, content.body, true, { name: "user_selections[#{content.id}]" } %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM