bigdaveyl
bigdaveyl

Reputation: 1391

How to link to a different controller in rails?

I am learning Ruby on Rails and I have a dumb question about the link_to function.

I set up a controller called "home" and I've created "index" as an action .

I've set up devise. However, when I browse to some links like sign_in, sign_up, etc. I get this error:

Routing Error
No route matches {:controller=>"devise/home"}

I've narrowed it down to:

<%= link_to "Home", { :controller => "home", :action => "index" }, :class => "navlink" %>

This works for my actions for home but not for my devise actions.

What am I missing here?

Upvotes: 1

Views: 973

Answers (1)

Russell
Russell

Reputation: 12439

Devise creates a set of helper functions for you that generate the correct paths. Run rake routes from the command line to see them. You'll get output similar to the following (assuming your Devise model is called User).

              new_user_session GET    /users/sign_in(.:format)                      {:controller=>"devise/sessions", :action=>"new"}
                  user_session POST   /users/sign_in(.:format)                      {:controller=>"devise/sessions", :action=>"create"}
          destroy_user_session GET    /users/sign_out(.:format)                     {:controller=>"devise/sessions", :action=>"destroy"}
                 user_password POST   /users/password(.:format)                     {:controller=>"devise/passwords", :action=>"create"}
             new_user_password GET    /users/password/new(.:format)                 {:controller=>"devise/passwords", :action=>"new"}
            edit_user_password GET    /users/password/edit(.:format)                {:controller=>"devise/passwords", :action=>"edit"}
                 user_password PUT    /users/password(.:format)                     {:controller=>"devise/passwords", :action=>"update"}
             user_registration POST   /users(.:format)                              {:controller=>"devise/registrations", :action=>"create"}
         new_user_registration GET    /users/sign_up(.:format)                      {:controller=>"devise/registrations", :action=>"new"}
        edit_user_registration GET    /users/edit(.:format)                         {:controller=>"devise/registrations", :action=>"edit"}
             user_registration PUT    /users(.:format)                              {:controller=>"devise/registrations", :action=>"update"}
             user_registration DELETE /users(.:format)                              {:controller=>"devise/registrations", :action=>"destroy"}

Appending _path to the first part of each line gives you the name of the helper function you need to call.

For example, the following would give you a link to the login page:

<%= link_to "Login", new_user_session_path %>

Remember if the request is a DELETE rather than a GET (such as the logout link) you'll need to specify this as part of the call to link_to.

<%= link_to "Logout", destroy_user_session_path, :method => :delete %>

Upvotes: 4

Related Questions