I’m trying to validate a record before save. I’m using one controller
in this case furnii where I have all my calls. The problem is that, the
form containing the field I’m trying to validate resides in the furnii
views. I tried to add validates_presence_of :content (the field name) in
both the furnii model and the ratecomment model but it does not work. I
know there are ways around this, suggestions anyone?
#########controller code
def rate
@furnii = Furni.find(params[:id])
Rating.delete_all([“rateable_type = ‘Furni’ AND rateable_id = ?”,
@furnii])
@furnii.add_rating Rating.new(:rating =>
params[:ratecomment][“rating”])
@comments = Ratecomment.new(params[:ratecomment])
if @comments.save
redirect_to furni_path(@furnii)
end
end
Sam,
If you have
class Ratecomment < ActiveRecord::Base
validates_presence_of :some_field
Then @comments.save should return false and the new instance of
Ratecomment not retained if :some_field is blank, regardless of what
controller it’s called in.
Is that what you mean and what is not happening?
On Aug 27, 12:56 pm, Sam G. [email protected]
smeade wrote:
Sam,
If you have
class Ratecomment < ActiveRecord::Base
validates_presence_of :some_field
Then @comments.save should return false and the new instance of
Ratecomment not retained if :some_field is blank, regardless of what
controller it’s called in.
Is that what you mean and what is not happening?
On Aug 27, 12:56�pm, Sam G. [email protected]
yes that is the issue.
########this is the model
class Ratecomment < ActiveRecord::Base
validates_presence_of :content
end
##############this is the view
<% form_tag( “rate?id=#{@furnii.id}”, :name => “theform”) do %>
<%= error_messages_for 'furni' %> |
Rate it: |
1 <%= radio_button "ratecomment",
"rating", "1", :checked => "checked"%> |
2 <%= radio_button "ratecomment",
"rating", "2" %> |
3 <%= radio_button "ratecomment",
"rating", "3" %> |
4 <%= radio_button "ratecomment",
"rating", "4" %> |
5 <%= radio_button "ratecomment",
"rating", "5" %> |
|
Add comment
<%= text_area :ratecomment, :content,
:rows=>“6”, :cols=>“60” %>
|
<%= link_to 'cancel''''', index_furnii_path()%> |
<%= submit_tag "Submit" %>
|
<%= hidden_field :ratecomment, :rateable_id, :value => @furnii.id %>
<% end %>
To clarify, is the Ratecomment getting created and saved or just the
Rating is getting applied to the furnii? How do you know the
Ratecomment is getting created, is it shown in another view or you see
it in the database?
On Aug 27, 2:23 pm, Sam G. [email protected]
The code does not appear to handle the case when @comments.save fails,
so the default is to display the view for rate, which probably does
not exist (it looks like rate is defined to handle only the posting of
the form). It seems likely that you need something like this for the
case when @comments.save fails:
render :action=>rate_form
On Aug 27, 2:05 pm, Sam G. [email protected]
smeade wrote:
To clarify, is the Ratecomment getting created and saved or just the
Rating is getting applied to the furnii? How do you know the
Ratecomment is getting created, is it shown in another view or you see
it in the database?
the Ratecomment gets created and saved in the database with a furnii_id.
I’m redirecting it to the view in the furnii and it shows up no problem.
the issue is if the user does not type anything in the text area field
and submits. The validation some how is trying to look for a view.