Reputation: 558
i have two tables in my application
rfp.rb
has_many :rfp_hors
attr_accessible :rfp_hors_attributes
accepts_nested_attributes_for :rfp_hors, :allow_destroy => true
rfp_hor.rb
attr_accessible :numberofmenu_est_hours,
:numberofmenu_act_hours,
:browser_est_hours,
:browser_act_hours,
:numberofpage_est_hours,
:numberofpage_act_hours,
:rfp_id
belongs_to :rfp
when i submit rfp_hors the parameter shows as follows in console
Parameters: {"rfp_hor"=>{"ecommerce_est_hours"=>"7", "rfp_id"=>"13", "designcomplexity_est_hours"=>"3", "browser_est_hours"=>"4", "framworks_est_hours"=>"5", "cms_est_hours"=>"6"}, "utf8"=>"✓", "commit"=>"Create Rfp hor", "authenticity_token"=>"XXgQlufpBP2lvcde/EiFIx93aM5Ov47MNFqsCkLun2Y="}
and controller rfps.rb
def show
@rfp = Rfp.find(params[:id])
@rfp_hor = RfpHor.new
end
rfp_hors.rb
def create
@rfp_hor = RfpHor.create(params[:rfp_hor])
respond_to do |format|
if @rfp_hor.save
format.html { redirect_to rfp_url(@rfp_hor.rfp_id), :notice => 'rfp hour was successfully created.' }
format.json { render :json => @rfp_hor, :status => :created, :location => @rfp_hor }
else
format.html { render :action => "new" }
format.json { render :json => @rfp_hor.errors, :status => :unprocessable_entity }
end
end
end
every thing saves fine in databse aceept rfp_id in rfp_hors any help would be great thanks in advance
Upvotes: 0
Views: 57
Reputation: 2538
your problem is because you are initializing the variable @rfp_hor
as new independent object in the rfps controller when would you initialize only the varbiale @rfp
, you could try of this way:
def edit
@rfp = Rfp.find(params[:id])
end
on your update action of the same controller, you don't have to change nothing, and you can put this code in your form:
<%= form_for @rfp do |f| %>
<%= f.fields_for : rfp_hors do |item| %>
<%= item.field_one :field %>
<%= item.field_two :field %>
<% end %>
<% end %>
in this way you can to receive the params as nested form in the same controller in the update action and you can show the params of this mode:
Parameters: {"rfp"=>{"rfp_hors_attributes"=>{"ecommerce_est_hours"=>"7", "rfp_id"=>"13", "designcomplexity_est_hours"=>"3", "browser_est_hours"=>"4", "framworks_est_hours"=>"5", "cms_est_hours"=>"6"}}, "utf8"=>"✓", "commit"=>"Create Rfp hor", "authenticity_token"=>"XXgQlufpBP2lvcde/EiFIx93aM5Ov47MNFqsCkLun2Y="}
Upvotes: 1