ga-wdi-exercises / project2

[project]
1 stars 30 forks source link

redirect after sign in #913

Closed colleeno closed 7 years ago

colleeno commented 7 years ago

I require uses to sign in to add a star to favorite a post ('script' in my project). Adding a star is defined on the script controller. Users are being dropped on the route page after sign in, I'd like to redirect users back to the script show page (nested under category) instead.

class ScriptsController < ApplicationController
before_action :authenticate_user!, only: [:new, :add_star, :remove_star]
def add_star
    @category = Category.find_by_name(params[:category_id]
    @script = Script.find(params[:id])
    @script.stars.create(user: current_user)
    redirect_to :back
end

I tried a redirect back to my script show route, but still drops me on the root.

I added this code to my application controller, which is storing and directing a user back to something like 'http://localhost:3000/categories/tv/quotes/323/add_star' but I don't have an actual route ending in /add_star since it's just an action on my script page. Is there a way to just drop them on 'http://localhost:3000/categories/tv/quotes/323'?

before_action :store_current_location, :unless => :devise_controller?
  private
  def store_current_location
    store_location_for(:user, request.url)
end
AndyWhitley commented 7 years ago

Hi Colleen, instead of using

redirect_to :back

couldn't you use

redirect_to category_script_path(@category, @script)
colleeno commented 7 years ago

tried that, still directs to the route after login even with 'redirect_to category_script_path(@category, @script)'

¯_(ツ)_/¯

not a major issue, just a bit of an odd user experience

AndyWhitley commented 7 years ago

Try getting rid of the code you added to ApplicationController as well. That is odd though, because typically devise will send you back to where you were going beforehand when it forces you to sign in.