Rails f.error_messages alwais empty
What did I do :)? In rails form has f.error_messages alwais em开发者_StackOverflow中文版pty. How can I fix/check this? Thx
The AR#validate method fills the model's error hash with validation errors.
If between instantiating the model and the call f.error_messages you do not call validate (via AR#save or directly) the @errors hash never gets filled and the errors are never shown).
Also make sure you do not redirect ( the validated object gets lost and a new one is created and has no "filled" @errors hash ), but call render :action => ...
Are you looking for error_messages_for :model?
After validation, this function will build a list of error messages for your view.
For example:
# users_controller.rb
def create
@user = User.new(params[:user])
if @user.save
redirect_to @user
else
render :action => 'new'
end
end
# view/users/new.html.erb
<%= error_messages_for :user %>
<% form_for @user do |f| %>
...
<% end %>
精彩评论