# Error\_messages\_for

**URL:** <https://www.ruby-forum.com/t/error-messages-for/98329>\
**Category:** Rails\
**Created:** [July 13, 2006, 10:52pm UTC](https://www.ruby-forum.com/t/error-messages-for/98329 "2006-07-13T22:52:14Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yngwie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/8dc957/32.png) [@Yngwie](https://www.ruby-forum.com/u/Yngwie)\
**Post date:** [July 13, 2006, 10:52pm UTC](https://www.ruby-forum.com/t/error-messages-for/98329/1 "2006-07-13T22:52:14Z")

</div>

I can’t seem to figure out how to validate a few fields some1 please  
help me out

model---------  
class Record \< ActiveRecord::Base  
validates\_presence\_of :sw, :flex  
end

controller------------  
class RecordController \< A;;icationController  
def new  
@record = Rrecord.new  
@product = Product.find(params[‘product\_id’])  
end

def create  
@Record.new(params[‘record’])  
if @record.valid?  
@record.save  
redirect\_to :action =\> ‘show’, :id = @record.product\_id  
else  
render\_action ‘new’  
end  
end  
end

view--------------  
\<%= error\_messages\_for ‘Record’ %\>

\<input id=“record\_sw” name = “record[sw]”,size = “40” type= “text” value  
=" " /\>  
\<input id=“record\_flex” name = “record[flex]”,size = “40” type= “text”  
value =" " /\>

Now i know the record will save and be created just fine w/out checking  
‘@record.valid?’, but when i do check, it fails (if the field is blank)  
and the render\_action ‘new’ complains that it is looking for the  
“product\_id”, which i understand because it is looking for it in the  
controller. Why won’t this work, i’ve spent well more time than  
allocated on such a small matter, please help!!!

---

<div class="post-metadata">

**Author:** ![Yngwie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/8dc957/32.png) [@Yngwie](https://www.ruby-forum.com/u/Yngwie)\
**Post date:** [July 14, 2006, 11:24am UTC](https://www.ruby-forum.com/t/error-messages-for/98329/2 "2006-07-14T11:24:29Z")

</div>

On 7/13/06, Yngwie [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> class RecordController \< A;;icationController  
> def new  
> @record = Rrecord.new  
> @product = Product.find(params[‘product\_id’])  
> end
> 
> def create

This line should read  
@record = Record.new(params[:record])

```
  @Record.new(params['record'])

```

> ```
> if @record.valid?
> @record.save
> redirect_to :action => 'show', :id = @record.product_id
> else
> render_action 'new'
> end
> 
> ```
> 
> end  
> end
> 
> view--------------

The error\_messages for looks for an instance variable with the same name  
as  
the string provided.

eg  
\<%= error\_messages\_for “record” %\> #=\> looks for @record and displays  
errors  
\<%= error\_messages\_for “my\_object” %\> #=\> looks for @my\_object and  
displays  
errors

Also in your controller your calling for params[:product\_id] which, from  
your view I’m guessing is not present.

See the name attribute of your hidden field. record[product\_id] This  
is  
what is submitted back to your controller.

you should have  
@product = Product.find(params[:record][:product\_id])

\<%= error\_messages\_for ‘Record’ %\>

> Now i know the record will save and be created just fine w/out checking  
> ‘@record.valid?’, but when i do check, it fails (if the field is blank)

When the save call is made, the object is checked for validity anyway.  
You  
can use something like  
if @object.save  
redirect\_to …  
…  
If it’s not valid, it won’t save

and the render\_action ‘new’ complains that it is looking for the

> “product\_id”, which i understand because it is looking for it in the  
> controller. Why won’t this work, i’ve spent well more time than  
> allocated on such a small matter, please help!!!

See above for product\_id parameter

–
