在 Rails 3.1.0 中修改传递给控制器的参数的正确方法是什么?
在我们的 Rails 3.1.0 应用程序中,我们需要修改创建和更新中传递给 rfq 控制器的参数。例如,我们想在input_by_id下记录当前用户id。我们所做的是:
@rfq.input_by_id = session[:user_id]
它按预期工作。另外,当 need_report 字段为 false 时,report_language 字段应该为零。我们决定在 rfq 控制器中添加以下行,以确保当 need_report 为 false 时,将 nil 传递给 report_language:
@rfq.report_language = nil unless params[:need_report]
但是,由于数据验证失败,此添加会导致 rspec 案例失败(在控制器的创建/更新中)。然而,当我们启动应用程序时,当 need_report 为 false 时,它表现良好,无需保存 report_language。我想知道上面的行是否不是使用 params[:need_report] 进行 @rfq 更新的正确方法。
非常感谢。
更新:
控制器代码:
def create
if has_create_right?
@rfq = Rfq.new(params[:rfq], :as => :roles_new )
@rfq.input_by_id = session[:user_id]
#save sales_id selected
if sales? && member? && !team_lead?
@rfq.sales_id = session[:user_id]
end
#view page may carry the hidden report language even if need_report == false
@rfq.report_language = nil unless params[:need_report]
#save into join table rfqs_standards
params[:rfq][:standard_ids].each do |sid|
@rfq.standards << Standard.find(sid.to_i) if !sid.nil? && sid.to_i > 0
end unless params[:rfq][:standard_ids].nil?
#save into join table rfqs_test_items
params[:rfq][:test_item_ids].each do |tid|
@rfq.test_items << TestItem.find(tid.to_i) if !tid.nil? && tid.to_i > 0
end unless params[:rfq][:test_item_ids].nil?
if @rfq.save!
redirect_to URI.escape("/view_handler?index=0&msg=RFQ saved!")
else
flash.now[:error] = "RFQ not saved!"
render 'new'
end
else
redirect_to URI.escape("/view_handler?index=0&msg=No rights!")
end
end
添加 @rfq.report_language = nil 后测试用例失败,除非 params[:need_report]
it "should be successful for corp head" do
session[:corp_head] = true
session[:user_id] = 1
s = Factory(:standard)
rfq = Factory.attributes_for(:rfq, :need_report => true, :report_language => 'EN')
rfq[:standard_ids] = [s.id] # attach standard_id's to mimic the POST'ed form data
get 'create', :rfq => rfq
#response.should redirect_to URI.escape("/view_handler?index=0&msg=RFQ saved!")
response.should render_template('new')
end
In our Rails 3.1.0 app, we need to modify params passed to rfq controller in create and update. For example, we want to record the current user id under input_by_id. What we did was:
@rfq.input_by_id = session[:user_id]
It worked as expected. Also when need_report field is false, then report_language field should be nil. We decide to add the following line in rfq controller to make sure the nil is passed to report_language when need_report is false:
@rfq.report_language = nil unless params[:need_report]
However this addition causes the rspec case failure (in create/update of the controller) because of the data validation failure. However when we fire up the app, it behaves fine without saving the report_language when need_report is false. I am wondering if the line above is not the right way to use params[:need_report] for @rfq updating.
Thanks so much.
UPDATE:
Controller code:
def create
if has_create_right?
@rfq = Rfq.new(params[:rfq], :as => :roles_new )
@rfq.input_by_id = session[:user_id]
#save sales_id selected
if sales? && member? && !team_lead?
@rfq.sales_id = session[:user_id]
end
#view page may carry the hidden report language even if need_report == false
@rfq.report_language = nil unless params[:need_report]
#save into join table rfqs_standards
params[:rfq][:standard_ids].each do |sid|
@rfq.standards << Standard.find(sid.to_i) if !sid.nil? && sid.to_i > 0
end unless params[:rfq][:standard_ids].nil?
#save into join table rfqs_test_items
params[:rfq][:test_item_ids].each do |tid|
@rfq.test_items << TestItem.find(tid.to_i) if !tid.nil? && tid.to_i > 0
end unless params[:rfq][:test_item_ids].nil?
if @rfq.save!
redirect_to URI.escape("/view_handler?index=0&msg=RFQ saved!")
else
flash.now[:error] = "RFQ not saved!"
render 'new'
end
else
redirect_to URI.escape("/view_handler?index=0&msg=No rights!")
end
end
Test case failed after addition of @rfq.report_language = nil unless params[:need_report]
it "should be successful for corp head" do
session[:corp_head] = true
session[:user_id] = 1
s = Factory(:standard)
rfq = Factory.attributes_for(:rfq, :need_report => true, :report_language => 'EN')
rfq[:standard_ids] = [s.id] # attach standard_id's to mimic the POST'ed form data
get 'create', :rfq => rfq
#response.should redirect_to URI.escape("/view_handler?index=0&msg=RFQ saved!")
response.should render_template('new')
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在于你根本没有看到正确的价值。
获取“创建”,:rfq => rfq
将产生类似于{:rfq =>; 的 params-hash {...}}
所以你需要
@rfq.report_language = nil 除非 params[:rfq][:need_report] == 'true'
the problem ist that you are simply not looking at the right value.
get 'create', :rfq => rfq
will result in a params-hash like{:rfq => {...}}
so you need to
@rfq.report_language = nil unless params[:rfq][:need_report] == 'true'