redirect_to and :controller, :actions getting mixed up in Rails
In a Rails controller I'm calling this:
redirect_to :controller => "user", :action => "login"
My 开发者_Go百科user_controller.rb
defines the following method:
class UserController < ApplicationController
def login
###
end
When I call my redirect_to
method I get this error in my browser: No action responded to show.
Checking the server logs I see this line:
Processing UserController#show (for 127.0.0.1 at 2009-12-19 12:11:53) [GET]
Parameters: {"action"=>"show", "id"=>"login", "controller"=>"user"}
Finally, I've got this line in my routes.rb
map.resources :user
Any idea what I'm doing wrong?
The hack-y answer is to change the following line in your routes.rb
map.resources :user
to
map.resources :user, :collection => { :login => :get }
But that's bad for a variety of reasons -- mainly because it breaks the RESTful paradigm. Instead, you should use a SessionController and use sessions/new
to login. You can alias it to /login
by the following line in routes.rb
map.login 'login', :controller=>"sessions", :action=>"new"
Tried
redirect_to :url => {:action => 'login', :controller => 'user'}
Or you might have to remove the routing or write your own. The routing maps POST to create, PUT to update etc.
You need to add the login
action in your routes.rb. Refer to this excellent guide.
What I ended up doing was creating a new login_controller
to handle logins.
Here's the relavent bit from my routes.rb file:
map.login '/login', :controller => 'login', :action => 'login'
Thanks for your help...
精彩评论