开发者

/session instead of /login when login attempt failed

sessions_controller.rb

  def create
    if user = User.authenticate(params[:login], params[:password])
      session[:user_id] = user.id
      redirect_to posts_path
    else
      render :action => 'new'
    end
  end


routes.rb

  get "sessions/create"
  get "sessions/destroy"
  get "sessions/new"

  resources :posts
  resource  :session
  resources :users

  match '/login',  :to => 'sessions#new',     :as => 'login'
  match 开发者_运维问答'/logout', :to => 'sessions#destroy', :as => 'logout'

Is it possible to keep the /login url after the render :action => "new" ??? thanks.


redirect_to '/login' does not keep the post information like render 'new' does.

I'm not completely happy with this solution, but this is what I have done:

resource :session, :only => [:create, :new, :destroy], 
         :path_names => { :new => 'login' }

Which gives you the following routes:

  • session POST /session(.:format) {:action=>"create", :controller=>"sessions"}
  • new_session GET /session/login(.:format) {:action=>"new", :controller=>"sessions"}
  • DELETE /session(.:format) {:action=>"destroy", :controller=>"sessions"}


The easy solution would be to simply change render :action => 'new' to redirect_to '/login'. I'm not amazingly fond of this, but it should solve the problem for you.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜