/ session instead of / login if login fails

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 save the / login URL after rendering: action => "new" ??? thank.

+3
source share
2 answers

redirect_to '/login'does not save information such as render 'new'.

I'm not quite happy with this solution, but this is what I did:

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"}
+1

render :action => 'new' redirect_to '/login'. , .

0

Source: https://habr.com/ru/post/1766058/


All Articles