Reputation: 110960
I've been banging my head against the wall try to understand how to get Devise to work with customer registration....
So On my landing page I want to show a registration form, so I added this to my view:
<%= render 'devise/registrations/new' %>
In that partial I have in the view a form tag like follows:
<%= form_for(user_registration_path, :url => user_registration_path) do |f| %>
.
.
In my application layout I have:
<% flash.each do |key, value| %>
<div class="flash <%= key %>"><%= value %></div>
<% end %>
The issue I'm having is when I submit a new registration form with invalid params, I don't see the error message?
But if I submit valid info the form does say it worked and that I need to check my email for the confirmation link, which is good.
Can you help me understand how to get this working end-2-end so I can display the errors:
Here's my full controller:
# GET /users/new
# GET /users/new.xml
# GET /users/new.json HTML AND AJAX
#-------------------------------------------------------------------
def new
respond_to do |format|
format.json { render :json => @user }
format.xml { render :xml => @user }
format.html
end
end
# GET /users/1/edit
# GET /users/1/edit.xml
# GET /users/1/edit.json HTML AND AJAX
#-------------------------------------------------------------------
def edit
respond_to do |format|
format.json { render :json => @user }
format.xml { render :xml => @user }
format.html
end
rescue ActiveRecord::RecordNotFound
respond_to_not_found(:json, :xml, :html)
end
# POST /users
# POST /users.xml
# POST /users.json HTML AND AJAX
#-----------------------------------------------------------------
def create
@user = User.new(params[:user])
if @user.save
respond_to do |format|
format.json { render :json => @user.to_json, :status => 200 }
format.xml { head :ok }
format.html { redirect_to :action => :index }
end
else
respond_to do |format|
format.json { render :text => "Could not create user", :status => :unprocessable_entity } # placeholder
format.xml { head :ok }
format.html { render :action => :new, :status => :unprocessable_entity }
end
end
end
The model:
validates :fname, :presence => true, :length => { :minimum => 2 }
validates :lname, :presence => true, :length => { :minimum => 2 }
validates :password, :presence => true, :length => { :minimum => 6 }
validates :email, :presence => true, :length => { :minimum => 6 }
Upvotes: 2
Views: 4477
Reputation: 9826
Just insert this at the beginning of your form, end before the end of it:
<%= devise_error_messages! %>
Devise is kinda tricky, I just finally managed to figure it out myself, and it's awesome. Btw, don't render the view as a partial, use templates, and then set your root to users#registrations_controller
, given that you created custom controllers and views.
If you haven't, I wrote a blog post about it here
Upvotes: 0
Reputation: 159
From the github readme: "Remember that Devise uses flash messages to let users know if sign in was successful or failed. Devise expects your application to call "flash[:notice]" and "flash[:alert]" as appropriate."
Upvotes: 1
Reputation: 42863
Where is your <%= error_messages_for %>
? That is how errors will be displayed.
Also, devise needs virtual attributes in the model for additional attributes so don't forget to add :lname
, :fname
etc... to the devise user model or whatever you are doing.
Update
<%= f.error_messages_for :model %>
Before Rails 3 this is how errors were formatted, but in R3 it's depircated and you need to install a plugin to access errors this way. See comments for the link to the plugin
Upvotes: 0