I have following code in my view:
<% @m1.map(&:id).each do |id|%>
<%= b.fields_for :modul1hours do |f| %>
<%= f.hidden_field :modul1_id, id %>
<%= f.text_field :module_est_hours, :size => 30 %>
</tr>
<% end %>
<%end%>
params passing in console
Parameters: {"authenticity_token"=>"LJ/ZME2lHZ7VwCDgPKX6OFe326fXSXo5UB4M0cPwbCE=", "esthour"=>{"rfp_id"=>"6", "ecommerce_est_hours"=>"", "modul1hours"=>{"module_est_hours"=>"3"}, "designpages_est_hours"=>"", "cms_est_hours"=>""}, "modul1_ids"=>["12", "13", "14"], "utf8"=>"✓", "project_id"=>"second", "commit"=>"Add Todo"}
Current user: admin (id=1)
modul1_ids is the hidden array based on that three text box is created but when i submit the page gives me:
ActionView::Template::Error (undefined method `merge' for 12:Fixnum):
in first textbox i passed 1 second 2 and in third 3
last value(3) isthe s passing that one can see in the console params module_est_hours"=>"3
, but what about rest two fields y not passing and whats the solution for an error. Please help me.
Edit 1
<% @m1.map(&:id).each do |id|%>
<%= b.fields_for :modul1hours do |f| %>
<%= hidden_field_tag "modul1_ids[]", id %>
<%= f.text_field :module_est_hours, :size => 30 %>
</tr>
<% end %>
<%end%>
this code does not give the error, but also value is not stored in modul1hours
table
The field of the modul1hours
table are:
integer :modul1_id
decimal :module_est_hours
decimal :module_act_hours
integer :esthours_id
]
.rb
belongs_to :esthour
attr_accessible :module_est_hours,:module_act_hours
and controller
Update
def new
@esthour = Esthour.new
@gg = @esthour.modul1hours.build
@project = params[:project_id]
@rfp = params[:rfp_id]
@m1 = Modul1.where(:rfp_id => @rfp.id)
respond_to do |format|
format.html # new.html.erb
format.json { render :json => @esthour }
end
end over Update # GET /project_todos/1/edit
def edit
@esthour = Esthour.find(params[:id])
end
def create
@project = params[:project_id]
@esthour = Esthour.new(params[:esthour])
user_params = params.select{|k,v| k.include?('esthour')}
respond_to do |format|
if @esthour.save
get_issue_attribute_param1(user_params)
format.html { redirect_to project_rfp_url(@project,@esthour.rfp_id), :notice => 'hours was successfully created.' }
format.json { render :json => @esthour, :status => :created, :location => @esthour }
else
format.html { render :action => "new" }
format.json { render :json => @esthour.errors, :status => :unprocessable_entity }
end
end
end
is there any build needed?eg Esthour.modul1hour.build in new def of controller coz record not saved in table?
view
<%= form_for @esthour,:rfp_id => @rfp.id,:project_id => @project do |b| %>
<%= b.hidden_field :rfp_id, :value => @rfp.id %>
<%= hidden_field_tag :project_id, @project %>
<table>
<tr> <td><b>Menutype </b></td>
<% if @rfp.menutype.present? %>
<td><%= @rfp.menutype %></td>
<td><%= b.number_field :menutype_est_hours %></td>
<% end %>
</tr>
<tr> <td> <b>Number of menu</b> </td>
<% if @rfp.numberofmenu.present? %>
<td><%= @rfp.numberofmenu %></td>
<td><%= b.number_field :numberofmenu_est_hours %></td>
<% end %>
</tr>
<tr>
<% @m1.map(&:id).each do |id|%>
<%= b.fields_for :modul1hours do |f| %>
<%= f.hidden_field :modul1_id, value => id %>
<%= f.text_field :module_est_hours, :size => 30 %>
</tr>
<% end %>
<% end %>
</table>
<%= b.submit 'Add Todo' %>
<% end %>
@esthour = Esthour.new
@gg = @esthour.modul1hours.build
@project = params[:project_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
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 %>
来源:https://stackoverflow.com/questions/20539040/pass-array-hidden-field-in-nested-model-rails