
resources :product do resources :salesend
销售所属的产品,如果没有产品,销售实例就不存在.
我可以使用link_to @product直接链接到产品:
<%= link_to @product.name,@product %>
这产生了
<a href="/products/3">StrawBerry Jam</a>
但是,如果我想为销售做类似的事情,我不能单独使用@sale.我必须参与该产品.这不起作用:
<%= link_to @sale.date,@sale %>
我必须使用这样的东西:
<%= link_to @sale.date,[@sale.product,@sale] %>
第一种情况不起作用,因为没有定义sale_path(只有product_sale_path).
我的问题是:我可以在Sale模型中添加一些东西,以便link_to(或url_for)在生成url时自动添加“parent”(在这种情况下为产品)吗?
我已经尝试过查看the implementation of url_for,我想我可以通过monkeypatching HelperMethodBuilder.url.handle_model_call来做到这一点,但如果有另一种方法,我宁愿不这样做.
resources :products do resources :sales,only: [:index,:new,:create]endresources :sales,only: [:show,:edit,:update,:destroy]
现在link_to @sale将起作用,你只需要涉及index,new,create的产品.
http://guides.rubyonrails.org/routing.html#nested-resources(向下滚动到浅层嵌套)
总结以上是内存溢出为你收集整理的ruby-on-rails – rails:有没有办法使用`link_to`自动使用嵌套资源?全部内容,希望文章能够帮你解决ruby-on-rails – rails:有没有办法使用`link_to`自动使用嵌套资源?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)