我正在开发一个使用RubyonRails的web应用程序。我读过一些"找不到'id'=(ActiveRecord::RecordNotFound)的***"帖子,但没有一个能解决我的问题。
我有一个HAML视图,它接受competitions_id参数并传递它。
= form_tag weigh_ins_path, :method => :post, :id => 'select_weigh_in_form' do
= label :weigh_in, :competition_id, 'Competition'
= select :weigh_in, :competition_id, options_from_collection_for_select(@competitions, 'id', 'name'), :include_blank => true
= submit_tag 'Select Competition & Week'
我有一个带有以下参数的请求,包括competitions_id,它不是nil或null或类似的东西:
{"utf8"=>"✓",
"authenticity_token"=>"***Some stuff***==",
"weigh_in"=>{"competition_id"=>"1"},
"commit"=>"Select Competition & Week"}
我有一个控制器,它需要competition_id参数:
def create
@competition = Competition.find params[:competition_id]
***some more stuff**
但是,当我用Cucumber测试时,我得到了这个错误:
And I press "Select Competition & Week"
Couldn't find Competition with 'id'= (ActiveRecord::RecordNotFound)
当我尝试使用网络应用程序时,我会遇到这个错误:
ActiveRecord::RecordNotFound in WeighInsController#create
Couldn't find Competition with 'id'=
因此,即使来自视图的POST请求正在发送一个参数,控制器也没有得到它?如何修复或解决此问题?
看起来competition_id
嵌套在您在问题中发布的请求中。尝试从如下参数中检索:
params[:weigh_in][:competition_id]