开发者

simple_form doesn't show validation error for one specific form

开发者 https://www.devze.com 2023-03-25 05:46 出处:网络
Here\'s my form: = simple_form_for [@post, @comment] do |f| = f.input :text, :label => false = f.button :submit

Here's my form:

= simple_form_for [@post, @comment] do |f| 
  = f.input :text, :label => false
  = f.button :submit

Comments are nested within Posts resources. Comments validation: validates :text, :length => { :minimum => 5 }

Comments controller:

def create
开发者_如何学运维  @post = Post.find params[:game_id]
  comment = @post.comments.build :user_id => current_user, :text => params[:comment][:text]

  comment.save
  redirect_to @post
end

The form itself works fine. If I enter more than 5 chars, the comment gets created. But if it's less, I just get redirected to posts#show and there are no validation errors in the form (I've checked the sources).

I also tried filling the devise registration form, and if it fails, I can see the errors.

I guess the problem is caused by the redirect. But anyways, I don't know how to fix it.


If you redirect the user, the errors are lost unless your application remembers them somehow. There are two possibilities to solve this problem:

  1. Don't redirect the user, but just use render to display the view with error messages. That's what Devise does, for example.

  2. Do redirect the user, but transfer along the error messages somehow. There are many ways to do this, for example by saving them, either as a simple string, or in some serialized form, in the flash. But this is usually more complicated and should only be done if the redirect is necessary.

0

精彩评论

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

关注公众号