I am using Devise in an app for the first time and I am having a weird problem I cant understand:
I've created my User model with Devise, and setup all the registration, user sign-up, etc, and everything works fine. In my application header, I have my nav menu (which is currently empty) and a partial with log in, sign out, etc:
_header.html.erb
<div class='topbar'>
<div class='container'>
<%= link_to 'My app', root_path, :class => 'brand' %>
<div class='nav'>
</div>
<%= render 'layouts/user_options' %>
</div>
</div>
_user_options.html.erb
<ul class='user-options'>
<% if user_signed_in? %>
<li><%= link_to "Signed in as #{current_user.username}", '#' %></li>
<li><%= link_to "Sign out", destroy_user_session_path, :method => :delete %></li>
<% else %>
<li><%= link_to "Sign up", new_user_registration_path %></li>
<li><%= link_to "Sign in", new_user_session_path %></li>
<% end %>
</ul>
Now I am adding the first link to the nav menu, if I use:
<%= link_to 'Tasks', '/tasks/index' %>
Everything is fine. BUT if I use:
<%= link_to 'Tasks', :controller => 'tasks', :action => 'index' %>
When I click on any of the Devise links, such as 'Sign in', I get 'No route matches {:controller=>"devise/tasks"}'
By request on the comments, here is the output of rake routes:
Tasks: TOP => routes => environment (See full trace by running task with --trace) MacBook-Pro-de-o:dearsherpa Oscar$ bundle exec rake routes
tasks_index GET /tasks/index(.:format) {:controller=>"tasks", :action=>"index"}
tasks_show GET /tasks/show(.:format) {:controller=>"tasks", :action=>"show"}
new_user_session GET /users/sign_in(.:format) {:action=>"new", :controller=>"devise/sessions"}
user_session POST /users/sign_in(.:format) {:action=>"create", :controller=>"devise/sessions"}
destroy_user_session DELETE /users/sign_out(.:format) {:action=>"destroy", :controller=>"devise/sessions"}
user_password POST /users/password(.:format) {:action=>"create", :controller=>"devise/passwords"}
new_user_password GET /users/password/new(.:format) {:action=>"new", :controller=>"devise/passwords"}
edit_user_password GET /users/password/edit(.:format) {:action=>"edit", :controller=>"devise/passwords"}
PUT /users/password(.:format) {:action=>"update", :controller=>"devise/passwords"}
cancel_user_registration GET /users/cancel(.:format) {:action=>"cancel", :controller=>"devise/registrations"}
user_registration POST /users(.:format) {:action=>"create", :controller=>"devise/registrations"}
new_user_registration GET /users/sign_up(.:format) {:action=>"new", :controller=>"devise/registrations"}
edit_user_registration GET /users/edit(.:format) {:action=>"edit", :controller=>"devise/registrations"}
PUT /users(.:format) {:action=>"update", :controller=>"devise/registrations"}
DELETE /users(.:format) {:action=>"destroy", :controller=>"devise/registrations"}
root / {:controller=>"pages", :action=>"home"
and here is routes.rb:
get "tasks/index"
get "tasks/show"
devise_for :users
root :to => 'pages#home'
Why is this?? What I am doing wrong?
Edit2 : By the way, I forgot mentioning that Task belongs_to User
, and the User model has been generated by devise. Which probably is whats causing this. Can't I access /tasks/index without /user/id/tasks/?
Edit : more weird stuff, I tried using resources :tasks in my routes file, instead of the 'get' routes for that controller, and now when I click on <%= link_to 'Tasks', :controller => 'tasks', :action => 'index' %>
it takes me to the show action. So something is really wrong with my routes, apparently.
The problem is with 'devise' namespace. When you use link creation this way:
<%= link_to 'Tasks', :controller => 'tasks', :action => 'index' %>
then the route is generated from the current namespace, which is 'devise' when you click any devise link.
To avoid this behavior, you can either use the route this way:
<%= link_to 'Tasks', :controller => '/tasks', :action => 'index' %>
or use helpers, which work correctly in this situation (tasks_index_path in case of 'get "tasks/index"' in routes.rb, or tasks_path in case of resources :tasks)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.