Rails form params changing in controller
I have a form:
<%= form_for(:report_main, :u开发者_如何学运维rl => {:action => 'exporttoxiccreate'}) do |f| %>
<%= collection_select(:waste, :code, Waste.find_all_by_istoxic(false), :id, :code, :include_blank => '') %>
<%= f.check_box(:q_pripadnost) %>
<%= f.text_field(:amount) %>
<% end %>
and this code in controller:
def exporttoxiccreate
@report = ReportMain.new
@reportexport = ReportExport.new
@reportparam = params[:report_main]
@report.waste_id = @reportparam.waste.code
@report.amount = @reportparam.amount
if @report.save
@reportexport.report_main_id = @report.id
else
redirect_to(:action => 'exporttoxicnew')
end
@reportexport.q_pripadnost = @reportparam.q_pripadnost
if @reportexport.save
redirect_to(:action => 'show', :id => @reportexport.id)
else
redirect_to(:action => 'exporttoxicnew')
end
end
I want to save in two tables, in two objects data from this form, and I need to separate params to manipulate with. I tried with this:
@reportexport.q_pripadnost = @reportparam.q_pripadnost
I want to set q_pripadnost field in @reportexport with some value from param.
Where I make mistake?
When you get params from a form in Rails, it comes in the form of a hash. For example:
params[:report_main][:waste]
params[:report_main][:amount]
So when you call @reportparam = params[:report_main]
, you are setting @reportparam
to a hash, but then you are trying to use it later like an object. For example, instead of @reportparam.q_pripadnost
, use @reportparam[:q_pripadnost]
.
You can take a closer look at your variable by temporarily changing your action to show a text version of the variable, for example:
def exporttoxiccreate
@reportparam = params[:report_main]
render :text => @reportparam.to_yaml
end
精彩评论