I would like to create a profile after an user signs up for my site using Devise. So in User model, I add:
after_create :build_spec
def build_spec
Spec.new(:user_id => :id)
end
I also have a views/layouts/_header.html.erb that has a link to Spec:
<%= link_to "Profile", spec_path(Spec.find_by_user_id(current_user.id)) %>
This creates trouble when an user just completed signup form and clicked sign up button. He will be directed to some page. At this moment, I think Spec object for this user has not been created, thus the link in the _header partial doesn’t work (nil object), and user got an error:
No route matches {:action=>"show", :controller=>"specs"}
(Obviously, I had SpecsController with action “show”)
My question is: How to I create Spec for signed-up user before he is redirected to the first page after the sign up (in order to make the link to Spec works)?
Comments and advices are much needed.
Override the after_sign_up action in the Devise RegistrationsController.
https://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-on-successful-sign-up-%28registration%29
Which would then look like this:
Then again, your method might work as well, if you just replaced
Spec.new(:user_id => :id)
bySpec.create(:user_id => :id)Spec.new just creates a new instance of Spec but does not actually save it.