0

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]
4
  • Does the view code on top, and view code on edit are the same? Commented Dec 12, 2013 at 9:20
  • no, difference is in hidden field plz note Commented Dec 12, 2013 at 9:29
  • Can you post your controller & model code please? Commented Dec 12, 2013 at 9:29
  • is there any build needed?eg Esthour.modul1hour.build in new def of controller Commented Dec 12, 2013 at 9:46

2 Answers 2

3

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 %>
Sign up to request clarification or add additional context in comments.

5 Comments

you just took my answer :).
NO ERROR BUT value is nt saved in db
I've just noticed your create action - could you please paste your whole view, as it seems you didn't build your form correctly.
is there any build needed?eg Esthour.modul1hour.build in new def of controller coz record not saved in table
Now even textbox not appear for nested model!
1

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:

"30" %>

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

8 Comments

Yep because you're doing it right. You didn't post the correct model or controller code, so I guessed it. The problem will be that your ActiveRecord objects will not be built - I'll post an update which should show the textbox
Okay, so can you explain what your project has_many of? Also, can you post your new fields_for code
class Modul1hour < ActiveRecord::Base belongs_to :esthour attr_accessible :module_est_hours,:module_act_hours end
class Esthour < ActiveRecord::Base belongs_to :rfp has_many :modul1hours accepts_nested_attributes_for :modul1hours end
Thanks for the updates! Okay, looking at it more, you've got a much more complicated variable setup than I presumed before. Can you post your new action from your controller again?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.