我的帐户索引页面列出了所有帐户,每个帐户都有一个到“ + Service”的链接; 这应该将用户引导到/My _ services/new 页面,并且已经预填充了具有适当 ID 的 account _ ID 字段,这取决于在帐户索引页面上单击了哪个链接。
我在每个页面的底部都安装了 debug (params) ,在我尝试的过程中,除了/my _ services/new 页面的参数中显示的: controller 和: action 之外,我没有得到其他任何东西。
我一直在尝试的链接是这样的:
link_to "+ Service", "my_services/new", :account_id => acct.id
然后我在服务控制器中也有逻辑:
def new
@my_service = MyService.new
if params[:account_id]
@my_service.account_id = params[:account_id]
end
end
有没有人能帮我找到合适的方法?我还没有能够得到它与一些讨厌的小粗糙的东西,我尝试了。
剪辑
事实证明,如果将来有人在寻找这个答案,那么嵌套资源(可能使用 routes.rb 中的 shallow: true
选项)似乎是最好的选择。我的路线 rb 现在看起来是这样的:
resources :accounts, shallow: true do
resources :services
end
我的链接现在是这样的:
<%= link_to "+ Service", new_service_path(:service => { :account_id => @account.id } ) %>