繁体   English   中英

Rails形式参数在控制器中更改

[英]Rails form params changing in controller

我有一个表格:

<%= form_for(:report_main, :url => {: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 %>

并且此代码在控制器中:

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

我想将这种形式的数据保存在两个表中,将数据保存在两个对象中,并且我需要分离参数以进行操作。 我尝试了这个:

@reportexport.q_pripadnost = @reportparam.q_pripadnost

我想在@reportexport中将q_pripadnost字段设置为param中的一些值。

我在哪里犯错?

当您从Rails的表单中获取参数时,它以哈希的形式出现。 例如:

params[:report_main][:waste]
params[:report_main][:amount]

因此,当您调用@reportparam = params[:report_main] ,您@reportparam设置为哈希值,但是稍后您将尝试像对象一样使用它。 例如,使用@reportparam[:q_pripadnost]代替@reportparam.q_pripadnost

您可以通过临时更改操作以显示变量的文本版本来仔细查看变量,例如:

def exporttoxiccreate
    @reportparam = params[:report_main]
    render :text => @reportparam.to_yaml
end

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM