开发者

Rails f.error_messages alwais empty

开发者 https://www.devze.com 2022-12-21 21:26 出处:网络
What did I do :)? In rails form has f.error_messages alwais em开发者_StackOverflow中文版pty. How can I fix/check this?

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 %>
0

精彩评论

暂无评论...
验证码 换一张
取 消