Rails is not validating

So I think its because I am merging a user id into the discussion upon create that does it, because I am validating both discussion content and title....

Discussion Model

class Discussion < ActiveRecord::Base  
  attr_accessible :user_id, :content, :title

  has_many :discussion_comments, :dependent => :destroy
  belongs_to :user

  validate :content, :presence => true,
                     :length => {:minimum => 10, :maximum => 254}
  validate :title, :presence => true,
                   :length => {:minimum => 10, :maximum => 254}

Discussion Controller

  def create
    @discussion = Discussion.create(params[:discussion].merge(:user_id => current_user.id))
    if @discussion.save
      redirect_to tasks_path, :flash => {:success => 'Created a new discussion'}
      redirect_to tasks_path, :flash => {:error => 'Cannot create empty discussions.'}

Any ways, Every time I try and save an empty form, it gives me the success message when it should give me the error message.

Discussion Form

<%= form_for @discussion do |f| %>

    <p><%= f.label :title %>
    <%= f.text_field :title %></p>

    <p><%= f.label :content %>
    <%= f.text_area :content %></p>

    <p><%= f.submit %></p>

<% end %>

As stated I think it has something to do with the fact that I am merging the user id upon create, how ever the validation should stop the whole create process - no?


Is this Rails 3.x? Try using validates not validate. Could just be a simple typo.


class Discussion < ActiveRecord::Base  

  validates :content, :presence => true,
                      :length => {:minimum => 10, :maximum => 254}
  validates :title, :presence => true,
                    :length => {:minimum => 10, :maximum => 254}

Ref: http://guides.rubyonrails.org/active_record_validations_callbacks.html#presence

I link to the presence example, but it should be validates regardless of which options you pass to it.

Need Your Help

Spring AOP: Performing Validations

java spring spring-aop

I am performing some validations in the Aspect having this code:

SELECT records WHERE rows have difference on a specific column

php mysql

I have a database table campaign_data. I need to select the customer_id where in the campaign there is difference in tariff. How can i do that with MySQL query. Here is some sample data.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.