【问题标题】:Trouble on setting the ':controller' parameter for a 'link_to' statement为 'link_to' 语句设置 ':controller' 参数时遇到问题
【发布时间】:2011-10-15 16:07:39
【问题描述】:

我正在使用 Ruby on Rails 3.0.9,我想知道为什么会出现下面描述的错误以及如何解决。

在我的/views/articles/categories/_content.html.erb 文件中,我有:

...
<%= link_to("New article", {:controller => content[:article_controller], :action => 'new'}) %>
...

如果我将content[:article_controller] 设置为(:only_path 选项同时设置truefalse

 1. content[:article_controller] = 'articles'
 2. content[:article_controller] = '/articles'
 3. content[:article_controller] = '/articles/'
 4. content[:article_controller] = '/'
 4. content[:article_controller] = ''

我分别得到以下错误(注意:controller 值):

 1. `ActionView::Template::Error (No route matches {:controller=>"articles/categories/articles", :action=>"new"})`
 2. `ActionView::Template::Error (No route matches {:controller=>"articles//articles", :action=>"new"})`
 3. `ActionView::Template::Error (No route matches {:controller=>"articles/", :action=>"new"})`
 4. `ActionView::Template::Error (No route matches {:controller=>"articles//", :action=>"new"})`
 4. `ActionView::Template::Error (No route matches {:controller=>"articles/categories/", :action=>"new"})`

这是 Ruby on Rails 的错误还是我的错? 问题出在哪里,我该如何解决,让 link_to 正常工作?

但是我可以使用以下方法解决这个问题:

<%= link_to("New article", {:controller => '../', :action => 'new'}) %>

但为什么它适用于 '.../' 而不是其他方式?


我注意到有时我尝试设置content[:article_contr8oller] 的控制器路径似乎依赖于处理视图文件的“基本”当前控制器路径(控制器文件是app/controllers/articles/categories/concerns_controller.rb - 请阅读下文了解更多信息)...为什么会这样?

使用url_for 也会发生这种情况:

url_for(:controller => 'articles', :action => 'new')

运行rake routes 命令我得到以下信息:

   articles_categories GET    /articles/categories(.:format)          {:action=>"index", :controller=>"articles/categories"}
                       POST   /articles/categories(.:format)          {:action=>"create", :controller=>"articles/categories"}
 new_articles_category GET    /articles/categories/new(.:format)      {:action=>"new", :controller=>"articles/categories"}
edit_articles_category GET    /articles/categories/:id/edit(.:format) {:action=>"edit", :controller=>"articles/categories"}
     articles_category GET    /articles/categories/:id(.:format)      {:action=>"show", :controller=>"articles/categories"}
                       PUT    /articles/categories/:id(.:format)      {:action=>"update", :controller=>"articles/categories"}
                       DELETE /articles/categories/:id(.:format)      {:action=>"destroy", :controller=>"articles/categories"}
              articles GET    /articles(.:format)                     {:action=>"index", :controller=>"articles"}
                       POST   /articles(.:format)                     {:action=>"create", :controller=>"articles"}
           new_article GET    /articles/new(.:format)                 {:action=>"new", :controller=>"articles"}
          edit_article GET    /articles/:id/edit(.:format)            {:action=>"edit", :controller=>"articles"}
               article GET    /articles/:id(.:format)                 {:action=>"show", :controller=>"articles"}
                       PUT    /articles/:id(.:format)                 {:action=>"update", :controller=>"articles"}
                       DELETE /articles/:id(.:format)                 {:action=>"destroy", :controller=>"articles"}

P.S.:如果您需要更多信息,请告诉我,我也会更新问题。


更新我

在我的路线文件中,我有:

namespace :articles do articles :categories end

scope :path => 'articles/categories/:id', :controller => 'articles/categories/concerns' do
  ...
end

resources :articles

更新 II

在我看来/views/articles/categories/_content.html.erb 我拥有的文件:

<div class="links">
  <%= link_to("New article", {:controller => content[:article_controller], :action => 'new'}) %>
</div>

在我的 Articles::Categories::ConcernsController 中(即在 app/controllers/articles/categories/concerns_controller.rb 文件中)我有:

def show
  @articles_category = Articles::Category.find(params[:id])

  respond_to do |format|
    format.html {
      render :partial => '/views/articles/categories/_content.html.erb',
        :locals  => {
          :content => {
             :article_controller => '/articles'
          }
        }
        format.js  {
          ...
        end
  end
end

【问题讨论】:

  • 我仍然不知道您在这里实际尝试做什么。请展示您创建内容哈希的完整部分和完整代码。
  • @Jeremy Weathers - 我重新更新了问题。

标签: ruby-on-rails ruby ruby-on-rails-3 routing controller


【解决方案1】:

你为什么不使用link_to 'New article', new_article_path?当您可以使用命名路径/url 帮助器 (new_article_url) 时,为什么还要使用旧的、疲惫的 url_for ...

【讨论】:

  • 这是因为我将一些 ':locals' 变量传递给部分模板文件。
  • 您拥有多个管理文章的控制器?为什么?我感觉到一些奇怪的,可能是你没有显示的臭代码。
  • 是的,你是对的,但是其他控制器不管理 Article 类。此外,提到的 Category 类是命名空间的,它只是在与 Article 相关的路由器之前的路由中声明。可能是问题吗?您需要了解哪些信息(我在 P.S. 的问题中写道)
【解决方案2】:

您尝试过使用符号吗?我认为他们更“直接”。

<%= link_to("New article", {:controller => content[:article_controller].to_sym, :action => :new}) %>

您是否尝试过使用相对路径?

<%= link_to("New article", {:controller => "../#{content[:article_controller]}", :action => 'new'}) %>

【讨论】:

  • 字符串或符号无所谓,相对路径也无所谓
  • @Koraktor - 使用符号我得到以下错误:'ActionView::Template::Error (can't convert Symbol into Integer)'。使用相对路径我得到以下错误:'ActionView::Template::Error (No route matches {:controller=>"articles/../articles", :action=>"new"})'
  • @Backo,当使用 url 哈希时,控制器需要是控制器的名称,而不是路径。从哈希(或者最好从命名路由)构建 url 的全部意义在于您不必担心相对路径。
  • @Jeremy Weathers - 我设置了控制器的名称(不是它的路径)。
  • @Backo,您的问题'/articles/' 和评论:controller=&gt;"articles/../articles" 中的这些示例都不是控制器名称 - 两者都是路径。这就是为什么他们不工作。我不知道为什么你有多个控制器来创建新文章,但如果你正在这样做,那么你要么需要传递一个有效的控制器名称(例如,'articles' 是指在 app/controllers/articles_controller.rb 中定义的 ArticlesController),或手动构建根相对 URL。同样,您似乎在这里与 Rails 作斗争,但请随时提供更完整的解释。
猜你喜欢
  • 1970-01-01
  • 2014-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-11
  • 2011-04-10
  • 2013-05-06
相关资源
最近更新 更多