简体   繁体   中英

Page redirect after user logs in

I'd like to be able to redirect the user to a different home page immediately after they log in (this part is working) but I would still like a logged in user to be able to access the index feed if they want. At the moment they can only see favourites_show_path but not @guidelines = Guideline.order(:title).all. Is there a way (without having to build a second index action and view) to get home page after login to direct to favourites_show_path but to still be able to see @guidelines = Guideline.order(:title).all?

 def index
    if params[:search].present?
    @search = Sunspot.search(Guideline) do  
      fulltext params[:search]
    end
    @guidelines = @search.results
  else
    redirect_to favourites_show_path, :action => 'index' and return if current_user
    @guidelines = Guideline.order(:title).all
  end

Just move the @guidelines... out of the condition and rename the search results' symbol:

def index
  @guidelines = Guideline.order(:title).all
  if params[:search].present?
    @search = Sunspot.search(Guideline) do  
      fulltext params[:search]
    end
    @results = @search.results
  else
    redirect_to favourites_show_path, :action => 'index' and return if current_user
  end
end

Thanks, I have solved this. I should have been adding the redirect onto the login action as suggested. So I've removed

redirect_to favourites_show_path, :action => 'index' and return if current_user

from my index action and instead added to my application_controller.rb

def after_sign_in_path_for(resource)
favourites_show_path
end

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