Show a modal right after signup?

2019-08-10 11:18发布

问题:

When a user signup at my page I want them to be redirected to the root_path(this I have figured out how to do in the user controller). But then I want a modal to show in front of this page(and this should only happen the first time the user see this root/home page(like a flash message).

Here is my create method in the user controller:

def create
    @user = User.new(params[:user])
    if @user.save
      sign_in @user
      redirect_to root_path
    else
      render 'new'
    end
  end

The modal is placed in app/views/layouts/_modal.html.erb. Does anybody know how to make this happen?

(Jepp, I am a newbie :)

回答1:

You mentioned flash, and I thought it would be a good idea to take advantage of Rails' prebuilt flash usage to display your modal.

def create
  @user = User.new(params[:user])
  if @user.save
    sign_in @user
    session[:modal] = true
    redirect_to root_path
  else
    render 'new'
  end
end

In your view:

<% if session[:modal] == true %>
  <%= render :partial => 'layouts/modal'%>
<% end %>