我试图使用Ruby on Rails将新的location
添加到restaurant
。使用new_foo_path添加具有外键关系的新记录(@bar)
在restaurants#show
行动我跑:
我也得到一个URL:http://localhost:3000/locations/new.1
,其中1
是餐厅的ID。但是新的位置表单未显示。
什么是轨道方式来处理这个简单的情况?是否正在使用new_FOO_path
甚至是正确的事情?
在这种情况下,我不应该有一个下拉选择餐厅,因为我希望最终用户只能够一个location
添加到自己的餐厅。我不知何故需要在添加位置表单中添加带餐厅ID的隐藏输入,并且还要验证后端的ID。
我似乎无法找到restaurant_id作为隐藏字段。如何在视图中访问它以在POST操作方法中设置正确的'restaurant_id'? – sergserg 2013-02-23 16:13:33
你不需要。使用嵌套路线,restaurant_id是路径的一部分。例如:'/ restaurants/1/locations/new'。确保您的位置在您的控制器中正确构建,例如'@location = @ restaurant.location.build',那么表单应该使用正确的路线。或者使用'form_for [@restaurant,@location]'。 – ashtom 2013-02-23 16:19:45