views:

92

answers:

2

I have form in views/users/show.html.erb

<% form_for [@user,Wall.new] do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :message %><br />
<%= f.text_area :message,:rows=>4 %>
</p>
<%= f.hidden_field :sender_id,:value=>current_user.id %>
<p><%= f.submit "Submit" %></p>
<% end %>

and I have this validation method in Models/wall.rb

validate :isfriend?

def isfriend?
errors.add(:message,'You have to be a friend to send message')  if !Friendship.find(:first,
:conditions=>"requester_id=#{self.sender_id} and accepter_id=#{self.receiver_id} and status='accepted'
or requester_id=#{self.receiver_id} and accepter_id=#{self.sender_id} and status='accepted'")
 end

Everything is fine validation is working and saving disable also it gives error message but it's not normal way it gives message like that

ActiveRecord::RecordInvalid in WallsController#create

Validation failed: Message You have to be a friend to send message

RAILS_ROOT: C:/Users/MaDOnos/Documents/NetBeansProjects/tttttt

How can show this error message in fair way.

A: 

I dont see what your question is? The AR save is failing with RecordInvalid like it should and you are getting the validation message you supplied.

cpjolicoeur
Question is there if you look carefuly. Normal validation error message http://justaddwater.dk/wp-content/uploads/2006/08/screenshot-localization-simplified-pirate-talk.pngI got validation error message like that http://farm4.static.flickr.com/3224/2848299089_f441133149.jpg
+1  A: 

Looks like you are calling save! (or create!) instead of save (or create). The former will raise an exception like you got, the later will return false with validation errors on the object.

Tor Erik Linnerud
@wall = @user.walls.create!(params[:wall])I use this what can I do ?
Use create instead of create!
Tor Erik Linnerud
Validation is working but now it doesn't give any error messages just refresh the page (with create).