【发布时间】:2017-03-27 23:31:01
【问题描述】:
我无法弄清楚让辅助路径起作用的魔力。我的用例是这样的:“mydomain/articles/some_slug”用于带有显示操作的文章控制器。给定的 URL 完美运行。但我不知道如何让 rails 的助手在这种情况下工作。
routes.rb:
get '/articles/:slug', to: 'articles#show', as: 'article'
--- or ---
get '/articles/:slug', to: 'articles#show'
--- or changing :slug to :id
rails 路线(这个和类似的变化与其他 --- 或 --- 条件):
articles GET /articles/:slug(.:format) articles#show
我收到未定义的方法“path_to_article”错误。在以下情况下会发生此错误:
我的.html.erb:
<%= path_to_article(locals[:article]) %>
--- or ---
<%= path_to_article(id: locals[:article].slug) %>
--- or ---
<%= path_to_article(slug: locals[:article].slug) %>
然而,这段代码运行良好,但我不喜欢它,这正是我想要避免的:
<a href="/articles/<%= locals[:article].slug %>">
我还尝试了复数和非复数组合,我可以在这些组合中以及几乎可以在网上提出或找到的任何其他随机想法。最后,我似乎无法弄清楚如何让rails 识别'path_to_article()'。
Rails 5.0.2
感谢您的帮助!
【问题讨论】:
标签: ruby-on-rails routes helpers