Devise - Error message not showing up - redirects to update instead of create
I've over written the registration controller and I'm trying to return to the form to create the user, but it only works partly.
def create
u = User.find_by_email(params[:user][:email])
...
if u && u.unregistered
self.resource = u
if resource.update_with_password(params[resource_name])
...
set_flash_message :notice, :updated if is_navigational_format?
...
sign_in(resource_name, resource)
if u.confirmed?
redirect_to consultations_path
else
redirect_to send_advisor_confirmation_path(u.id)
end
else
clean_up_passwords(resource)
# this is the place that has a problem
respond_with_navigational(resource) { render_with_scope :new }
end
return
end
super
end
(side note: in my app it is po开发者_C百科ssible to create a user who hasn't signed in yet, they are defined as being "unregistered", and they can claim their account by going through the sign up process).
This respond_with_navigational works the first time (when it is new
posting to create
) but then when you mess the form up again it clears the whole form (when create
is posting to create
... or should be). The logs say that the first time it is going to create, but the second time it is going to update:
Started POST "/users" for 127.0.0.1 at 2011-07-20 15:49:30 -0500
Processing by RegistrationsController#create as HTML
...
Started POST "/users" for 127.0.0.1 at 2011-07-20 15:50:56 -0500
Processing by RegistrationsController#update as HTML
According to the routes (rake routes):
POST /users(.:format) {:action=>"create", :controller=>"users"}
new_user GET /users/new(.:format) {:action=>"new", :controller=>"users"}
edit_user GET /users/:id/edit(.:format) {:action=>"edit", :controller=>"users"}
user GET /users/:id(.:format) {:action=>"show", :controller=>"users"}
PUT /users/:id(.:format) {:action=>"update", :controller=>"users"}
So why would the send post be directed to the update? And how can I fix this?
Update: Ok, so it is being redirected to update (I believe) because resource is being defined as an existing object. But because I have my custom strategy for devise saying that this particular user isn't authenticated, then it is unable to go to update (which requires authentication) and is redirected to new, but without parameters.
So the question becomes, how do I setup resource so it looks like a new user, but has the errors that it has before creating the new user.
For example an error as is might be "Passwords not matching", but if you find the errors on a new user it would be "Email already taken".
EDIT 1: I missed some of your question, this may not be helpful.
Make sure you're using something like this to display it on your page:
layouts/application.html.erb (or wherever you need it)
<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>
I don't like this, but it works. If anyone has a better way of doing this, please let me know. Thank you.
...
else
resource.unregistered_advisor = true
clean_up_passwords(resource)
a = User.new(:email => resource.email, :name => resource.name, :confirmation_token => resource.confirmation_token)
resource.errors.each do |key,value|
a.errors[key] = value
end
self.resource = a
respond_with_navigational(resource) { render_with_scope :new }
end
...
精彩评论