I have following code in my view:
<% @m1.map(&:id).each do |id|%>
<%= b.fields_for :modul1hours do |f| %>
<%= f.hidden_field :mo
In this line:
<%= f.hidden_field :modul1_id, id %>
You are saying that you want the hidden field binded with modul1hour modul1_id
method and options being id
. Second parameter for FormBuilder hidden_field is expected to be a hash (which is then merged against default options). To do what you want do:
<%= f.hidden_field :modul1_id, value: id %>
Hidden fields aren't really the issue here
Apart from @BroiStatse's answer, I can see the issue as how you handle the params on your controller
Nested Models
Sending data to a controller sends that data to the relevant models. This is normally handled with accepts_nested_attributes_for, but can be handled manually too
From your controller code, I can't see how you're dealing with your extra data, but your error is caused by the incorrect merge
of the params
Instead of saving the data manually, I would use the accepts_nested_attributes_for
to save the data, like this:
#app/models/project.rb
Class Project < ActiveRecord::Base
accepts_nested_attributes_for :modul1hours
end
This will pass the params to your modul1hours model, where you'll then have to capture them with the relevant attr_accessible
actions
f.fields_for
In order to get accepts_nested_attributes_for
working properly, you have to ensure you use the f.fields_for
function correctly.
You have to first build
the ActiveRecord objects in your new
controller action, like this:
def new
@project = Project.new
@project.modul1hours.build
end
Your problem is that you're then cycling through the ID's of your modul1hours model, yielding the f.fields_for
artificially. Rails will only output an f.fields_for
if the ActiveRecord object has been built in the controller:
This RailsCast gives you a better idea about this
What I would do is this:
#app/controllers/projects_controller.rb
def new
@project = Project.new
@m1.map(&:id).each do |i|
@project.modul1hours.build
end
end
#app/views/projects/new.html.erb
<%= b.fields_for :modul1hours do |f| %>
<%= hidden_field_tag :id, value :id %>
<%= f.text_field :module_est_hours, :size => "30" %>
<% end %>
I'm still thinking about how I would assign the ID's to the hidden field
Update
Try this:
#app/controllers/projects_controller.rb
def new
@project = Project.new
@project.modul1hours.build
end
Replace modul1hours
with whatever your projects has_many
of