设置’link_to’语句的’:controller’参数时出现问题
我正在使用Ruby on Rails 3.0.9,我想知道为什么我得到下面描述的错误,我该如何解决。
在我的/views/articles/categories/_content.html.erb
文件中,我有:
... 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
正常工作的问题?
但是,我可以使用以下方法解决该问题:
'../', :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"}
PS:如果您需要更多信息,请告诉我,我也会更新问题。
更新我
在我的路线文件中,我有:
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
文件中我有:
content[:article_controller], :action => 'new'}) %>
在我的文章:: 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
你尝试过使用符号吗? 我认为他们更“直接”。
<%= link_to("New article", {:controller => content[:article_controller].to_sym, :action => :new}) %>
你尝试过使用相对路径吗?
<%= link_to("New article", {:controller => "../#{content[:article_controller]}", :action => 'new'}) %>
你为什么不使用link_to 'New article', new_article_path
? 为什么要使用旧的,累了的url_for ...
当你可以使用指定的路径/ url帮助器( new_article_url
)时。