【发布时间】: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 选项同时设置true 和false)
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