简体   繁体   中英

Redirect Logged In User to Associated Company Show Page

I have a UserSessionsController:

  def create
    @company = Company.find(params[:user_id])

    if login(params[:email], params[:password])
      redirect_back_or_to(users_path, message: "Logged in successfully.")
    else
      flash.now.alert = "Login Failed."
      render action: :new
    end
  end

I want to redirect the logged in user to the associate company's show page. Any ideas on what should replace users_path ?

I'm just brainstorming with the @company bit.

From redirect_back_or_to method name, I'll assume you're using Sorcery gem. Please provide such info in your questions if you want to get an accurate answer.

A quick look at methods source code shows that it's just a helper method around redirect_to controller method.

Therefore, you can just change your line from:

redirect_back_or_to(users_path, message: "Logged in successfully.")

To:

redirect_back_or_to(@company, message: "Logged in successfully.")

Again, assuming your Company resource isn't nested...

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