设置'link_to'语句的':controller'参数时出现问题

时间:2011-07-29 04:39:25

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

我正在使用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]设置为(true选项设置false:only_path

 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>

在我的文章:: 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

2 个答案:

答案 0 :(得分:0)

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

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

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

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

答案 1 :(得分:0)

为什么不使用link_to 'New article', new_article_path?当您可以使用指定的路径/网址助手(url_for ...)时,为什么要使用旧的,累了的new_article_url