# Validation question

**URL:** <https://www.ruby-forum.com/t/validation-question/147035>\
**Category:** Rails\
**Created:** [August 27, 2008, 8:56pm UTC](https://www.ruby-forum.com/t/validation-question/147035 "2008-08-27T20:56:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![staypufd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecd19e/32.png) [@staypufd](https://www.ruby-forum.com/u/staypufd)\
**Post date:** [August 27, 2008, 8:56pm UTC](https://www.ruby-forum.com/t/validation-question/147035/1 "2008-08-27T20:56:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![staypufd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecd19e/32.png) [@staypufd](https://www.ruby-forum.com/u/staypufd)\
**Post date:** [August 27, 2008, 10:18pm UTC](https://www.ruby-forum.com/t/validation-question/147035/2 "2008-08-27T22:18:04Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![staypufd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecd19e/32.png) [@staypufd](https://www.ruby-forum.com/u/staypufd)\
**Post date:** [August 27, 2008, 10:23pm UTC](https://www.ruby-forum.com/t/validation-question/147035/3 "2008-08-27T22:23:57Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

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: &nbsp; &nbsp;&nbsp; | 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 %\>

---

<div class="post-metadata">

**Author:** ![staypufd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecd19e/32.png) [@staypufd](https://www.ruby-forum.com/u/staypufd)\
**Post date:** [August 27, 2008, 10:50pm UTC](https://www.ruby-forum.com/t/validation-question/147035/4 "2008-08-27T22:50:17Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![staypufd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecd19e/32.png) [@staypufd](https://www.ruby-forum.com/u/staypufd)\
**Post date:** [August 28, 2008, 5:32pm UTC](https://www.ruby-forum.com/t/validation-question/147035/5 "2008-08-28T17:32:15Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![staypufd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecd19e/32.png) [@staypufd](https://www.ruby-forum.com/u/staypufd)\
**Post date:** [August 27, 2008, 11:05pm UTC](https://www.ruby-forum.com/t/validation-question/147035/6 "2008-08-27T23:05:52Z")

</div>

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.
