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}
end
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'}
else
redirect_to tasks_path, :flash => {:error => 'Cannot create empty discussions.'}
end
end
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
validatesnotvalidate. Could just be a simple typo.So…
Ref: http://guides.rubyonrails.org/active_record_validations_callbacks.html#presence
I link to the
presenceexample, but it should bevalidatesregardless of which options you pass to it.