0

I have 2 form, I want to create in my one form check_box_tag and if I checked same item, this item put in another form in hiden field tag, and when I save my another form value this item save in my database.

in my view

<% for item in @items %>
  <%= check_box_tag "items[#{item.id}]", item.id, :name => "items[]" %>    
<% end %>

<% form_for(@feedback ) do |f| %>

<%= f.hidden_field :item_value %> #this is field in which I want put from one form

<%= f.label :name, "Name" %></td>
<%= f.text_field :name, :style => "width:100%" %>

<%= f.label :phone, "Phone" %>
<%= f.text_field :phone, :style => "width:100%" %>

<%= f.label :email, "Email" %>
<%= f.text_field :email, :style => "width:100%" %>

<%= f.submit "Submit" %>

<% end %>

in my controller

...
@item = Item.find(:all)
@feedback = Feedback.new(params[:feedback])

email = ApplicationMailer.create_feedback_rez(@feedback)
email.set_content_type("text/html; charset=utf-8")
ApplicationMailer.deliver(email)

@feedback.save
...

How can I do it?

Thanks for advice!

4

2 回答 2

0

你在谈论的是所谓的嵌套表单 这是一个很好的使用它们的链接http://createdbypete.com/articles/working-with-nested-forms-and-a-many-to-many-association-in- rails-4/

于 2013-09-30T15:01:35.130 回答
0

在我看来,您似乎会从建立数据库关系中受益更多。创建一个反馈有很多项目的关系怎么样?然后你可以保持你的表单分开,让你的复选框表单在项目表中保存一个数据库记录,并通过它的 ID 将它链接到反馈表?根据您想要使用隐藏标签的原因,如果您这样做,您甚至可能不需要。如果这不是您要寻找的,那么更详细一点如何-您要完成什么?你的代码是做什么的?

于 2013-09-30T13:19:41.673 回答