我的帐户索引页面列出了所有帐户,每个帐户都有指向“ +服务”的链接;这应将用户定向到/ my_services / new页面,并根据在Accounts索引页面上单击的链接,用适当的ID预先填充account_id字段。
我在每个页面的底部都有debug(params),根据我的尝试,除了/ controller和:action外,/ my_services / new页面的参数中没有显示其他内容。
我一直在尝试的链接是这样的:
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
有人可以帮助您采取适当的方法吗?我还无法通过我尝试过的一些讨厌的小技巧来解决这个问题。
编辑
事实证明,如果将来有人在看这个答案,嵌套资源(可能带有shallow: true
routes.rb中的选项)似乎是解决之道。我这部分的routes.rb现在看起来像这样:
resources :accounts, shallow: true do
resources :services
end
我的链接现在看起来像这样:
<%= link_to "+ Service", new_service_path(:service => { :account_id => @account.id } ) %>