簡體   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