简体   繁体   中英

after_sign_in_path_for

I use devise in my application.

I want to pop-up a welcome message while the user login.

so in my application_controller.erb I defined:

class ApplicationController < ActionController::Base
  protect_from_forgery
  before_filter :authenticate_user!

  def after_sign_in_path_for(user)
    alert('Welcome!')
  end

  def after_sign_out_path_for(user)
    new_user_session_path
  end
end

when I tried to sign-in to my app, I got an error:

ArgumentError in Devise::SessionsController#create

wrong number of arguments (1 for 0)
Rails.root: /home/alon/alon/todolist

Application Trace | Framework Trace | Full Trace
app/controllers/application_controller.rb:14:in `after_sign_in_path_for'

By default devise adds flash messages. No need to set the flash message.Just you need to display the flash message in the view. Try the below code.

in your app/views/layouts/application.html.erb

<% flash.each do |type, message| %>
  <div class="flash">
     <%= message %>
  </div>        
<% end %> 

FYI after_sign_in_path_for is not for setting the flash message. Its the just to inform the path to devise where you want to redirect the application after successful login.

Lets set the successful login redirect path

in you config/routes.rb

match "users/dashboard" => "controllername#action"

And finally change the after_sign_in_path_for method

def after_sign_in_path_for(user)
  users_dashboard_path
end

You are calling javascript function 'alert()' from rb file. You should define path in

  def after_sign_in_path_for(user)
    some_path
  end

and use alert() in views with javascript_tag

Here is best way I have found to do alert messages if you have Bootstrap installed.

Add this inside your app/views/layouts/application.html.erb Right above <%= yield %>

<% if notice %>
  <p class="alert alert-success"><%= notice %></p>
<% end %>
<% if alert %>
  <p class="alert alert-danger"><%= alert %></p>
<% end %>


I got this from RailsGirls

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM