Question

I am working through the RailsApps Stripe tutorial.

When a new subscriber is created through a devise registration controller they are then directed to their content page through a content controller. I want to use their name and email address created upon registration on their content page. But I can't seem to bring the params into the content controller.

I put@user = User.find(params[:id]) into the content_controller but I get the error "Couldn't find User without an ID".

On the error page it lists under Request Info > rack session: "warden.user.user.key"=>["User", [2],

So does that mean that ID of User #2 is being passed to the content_controller but that I can't access it?

Was it helpful?

Solution

I put@user = User.find(params[:id]) into the content_controller but I get the error "Couldn't find User without an ID".

This error means that params[:id] = nil, i.e., you are not passing id in the params hash when redirecting the user to content page.

Possible Solutions:

  1. With Devise you get a helper method called current_user which returns an instance of currently logged in user. So, you could directly use current_user to access the attributes of currently logged in user. For example:

    To access name and email fields of currently logged in user, all you need to do is

    current_user.name
    current_user.email
    
  2. In routes.rb, pass the id of the currently logged in user via the route of content page as below:

    ##  Add :id dynamic segment to your content page route  
    get "content/:id", :to => "contents#action_name", :as  => "content_page"
    

    Since, I don't have the route details, you would need to modify the above route as per your requirement.

    Next, when redirecting the user to content page after sign up just pass the currently logged in user as below in ApplicationController method named after_sign_up_path_for (You need to override this Devise method if you want to redirect the user to a different route than the default root path):

    def after_sign_up_path_for(resource)
      content_page_path(resource) ## Provide the path and pass resource to it
    end
    

OTHER TIPS

With Devise, you can access the currently logged in user via the current_user helper in your controller.

See documentation: https://github.com/plataformatec/devise#controller-filters-and-helpers

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top