开发者

Trouble on setting the ':controller' parameter for a 'link_to' statement

开发者 https://www.devze.com 2023-03-23 22:05 出处:网络
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.

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 setting true and false 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 is app/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  {
          ...
开发者_如何学Python        end
  end
end


Did you try using symbols? I think they are more "direct".

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

Did you try using a relativ path?

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


Why aren't you using link_to 'New article', new_article_path? Why use the old, tired url_for ... when you can use the named path/url helper (new_article_url).

0

精彩评论

暂无评论...
验证码 换一张
取 消