设置'link_to'语句的':controller'参数时遇到问题 [英] Trouble on setting the ':controller' parameter for a 'link_to' statement
问题描述
我使用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.内容[:article_controller] =''
我分别得到以下错误(注意 :controller
values):
1.`ActionView :: Template :: Error {:controller =>articles / categories / articles,:action =>new})`
2.`ActionView :: Template :: Error(没有路由匹配{:controller =文章/文章,:action =>new})`
3. ActionView :: Template :: Error(没有路由匹配{:controller =>articles /,:action = ;new})`
4.`ActionView :: Template :: Error(没有路由匹配{:controller =>article //,:action =>new})`
4.`ActionView :: Template :: Error(没有路由匹配{:controller =>articles / categories /,:action =>new})`
是Ruby on Rails错误还是我的错?问题是什么,
link_to
正常工作?
但我可以通过使用:
<%= link_to(New article,{:controller => '../',:action => 'new'})%>
但是为什么它适用于
'... /'
但不是其他方式?
我注意到,我尝试设置
content [:article_contr8oller]
似乎依赖于正在处理视图文件的base当前控制器路径(控制器文件app / controllers / articles / categories / concerns_controller.rb
- 阅读下面的更多信息)...为什么会发生?
它也发生在使用
url_for
:url_for(:controller => 'articles',:action =>'new')
rake routes 命令我得到以下:
article_categories GET /articles/categories(.:format) {:action =>index,:controller =>articles / categories}
pre>
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}
article_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 =
文章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}
文章GET /articles/:id(.:format) {:action =>show,:controller =>articles}
PUT /articles/:id(.:format){:action =>update,:controller = article}
DELETE /articles/:id(.:format){:action =>destroy,:controller =>articles}
PS:如果您需要更多信息,请告诉我,我也会更新问题。
UPDATE I
在我的路线文件中,我有:
命名空间:articles do文章:类别end
scope:path => 'articles / categories /:id',:controller => 'articles / categories / concern'do
...
end
资源:articles
UPDATE II
c $ c> /views/articles/categories/_content.html.erb 档案:
< div class =links>
<%= link_to(新文章,{:controller => content [:article_controller],:action =>'new'})%>
< / div>
在我的文章:: Categories :: ConcernsController(也就是在
app / controllers / articles / categories / concern_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_path
?为什么在使用命名的路径/ url帮助器(new_article_url
)时,使用旧的,累了的url_for ...
I am using Ruby on Rails 3.0.9 and I would like to know why I get the error described below and how can I solve that.
In my
/views/articles/categories/_content.html.erb
file I have:... <%= link_to("New article", {:controller => content[:article_controller], :action => 'new'}) %> ...
If I set the
content[:article_controller]
to (both settingtrue
andfalse
for the:only_path
option)1. content[:article_controller] = 'articles' 2. content[:article_controller] = '/articles' 3. content[:article_controller] = '/articles/' 4. content[:article_controller] = '/' 4. content[:article_controller] = ''
I get respectively the following errors (note
:controller
values):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"})`
Is it a Ruby on Rails bug or is it my fault? What is the problem and how can I solve that making the
link_to
properly work?However I can solve that problem by using:
<%= link_to("New article", {:controller => '../', :action => 'new'}) %>
But why it works with
'.../'
but not in other ways?
I noticed that some time the controller path for which I try to set the
content[:article_contr8oller]
seems to relying on the "base" current controller path that is handling the view file (the controller file isapp/controllers/articles/categories/concerns_controller.rb
- read below for more information)... why it happens?It also happens using
url_for
:url_for(:controller => 'articles', :action => 'new')
Running the
rake routes
command I get the following: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.: If you need more information, let me know and I will update the question as well.
UPDATE I
In my route file I have:
namespace :articles do articles :categories end scope :path => 'articles/categories/:id', :controller => 'articles/categories/concerns' do ... end resources :articles
UPDATE II
In my view
/views/articles/categories/_content.html.erb
files I have:<div class="links"> <%= link_to("New article", {:controller => content[:article_controller], :action => 'new'}) %> </div>
In my Articles::Categories::ConcernsController (that is, in the
app/controllers/articles/categories/concerns_controller.rb
file) I have: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
解决方案Why aren't you using
link_to 'New article', new_article_path
? Why use the old, tiredurl_for ...
when you can use the named path/url helper (new_article_url
).这篇关于设置'link_to'语句的':controller'参数时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!