开发者

Rails validates_presence_of not working with HTML form text area

Environment: Rails 2.3.11 w/ MySQL 5.0

Here is my slideshow model:

class Slideshow < ActiveRecord::Base
  validates_presence_of :title, :description
end

Using开发者_如何学运维 the console, if I run:

Slideshow.new(:title => "", :description => "").save!

it returns:

Validation failed: Title can't be blank, Description can't be blank

which is correct.

However, when I submit a blank HTML form to the create action:

def create
  @slideshow = Slideshow.new(params[:slideshow])
  if @slideshow.save
    redirect_to(...)
  else
    render(:action => 'new')
  end
end

only the :title field fails validation. I've verified that what is being passed in the params is:

Parameters: {"commit"=>"Submit", "slideshow"=>{"title"=>"", "description"=>""}, "action"=>"create", "controller"=>"manage/slideshows"}

Why is the description field NOT failing validation here?

Thanks.


Try this :

validates_length_of :description

for more details ... http://apidock.com/rails/ActiveRecord/Validations/ClassMethods/validates_length_of


when You submit a blank HTML form to the create action, it should go in else of create action and your form should have this line:

<%= f.error_messages %>

to show you the errors.


This turned out to be a syntax issue. It was occurring on a testing server where there were two models with very similar names (one an updated version of the other). During testing I used the wrong one. My apologies for any unnecessary head-scratching :)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜