Context对于自行车订购系统来说,用户可以首先填写bike_type (例如‘山地车’),然后选择自行车(例如‘黄色山地车’)。
当用户输入bike_type但忘记自行车时,表单需要再次呈现,这样用户就可以填写丢失的自行车。
问题自行车订单模式
->因此,当用户第一次填写bike_type时,这个bike_type需要一辆自行车才能连接到订单。
如何在创建控制器中获取bike_type,以便在表单未正确填写时自动呈现最后一个填入的bike_type?
发送参数
{"utf8"=>"✓",
"authenticity_token"=>"NqEb3EhNDOYFI12tYLCp9akDtVnEbiA4skR5qmVygwnRv+GkELvDTEJhU8/o5Orvmsiaxk7PIPbawD9CZvWLYw==",
"order"=>
{"order_bikes_attributes"=>{"0"=>{"bikes"=>{"bike_type"=>"166"}, "bike_id"=>"Select bike"}},
"arrival"=>"",
"departure"=>"",
"order_contact_attributes"=>{"first_name"=>"", "last_name"=>"", "street"=>"", "street_number"=>"", "zipcode"=>"", "city"=>"", "country"=>"", "email"=>"", "phone"=>""}},
"commit"=>"Save & proceed to additional options",
"bike_store_id"=>"21"}发布于 2019-10-03 08:59:45
您可以从params抓取bike_type,如下所示:
params[:order][:order_bikes_attributes]['0'][:bikes][:bike_type]
发布于 2019-10-03 11:09:50
我建议您使用挖掘方法来避免nil错误。
params.dig('order', 'order_bikes_attributes', '0', 'bikes', 'bike_type')https://stackoverflow.com/questions/58214647
复制相似问题