# Problema con error\_messages\_for

**URL:** <https://www.ruby-forum.com/t/problema-con-error-messages-for/134538>\
**Category:** IT\
**Created:** [April 11, 2008, 8:40pm UTC](https://www.ruby-forum.com/t/problema-con-error-messages-for/134538 "2008-04-11T20:40:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mckracken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5daacb/32.png) [@mckracken](https://www.ruby-forum.com/u/mckracken)\
**Post date:** [April 11, 2008, 8:40pm UTC](https://www.ruby-forum.com/t/problema-con-error-messages-for/134538/1 "2008-04-11T20:40:39Z")

</div>

Ho un problema. Non viene visualizzato il rapporto degli errori riguardo  
alla presenza dei due campi autore e testo.  
Codice:

class Comment \< ActiveRecord::Base  
belongs\_to :post

```
validates_presence_of :autore, :testo

```

end

codice appartenente al controller ‘home\_controller’

def save\_comment  
if request.post?  
@comment = Comment.new(params[:comment])  
if @comment.save  
flash[:notice] = “Commento inserito con successo”  
redirect\_to("/home/show\_comment/" +  
params[:comment][:post\_id])  
else  
redirect\_to("/home/add\_comment/" + params[:comment][:post\_id])  
end  
end  
end

pagina add\_comment.html.erb

# Blog &amp News
 \<%= error\_messages\_for 'comment' %\> \<% for post in @posts %\> 

Titolo: \<%= post.titolo %\> data: \<%= post.data %\>

Testo:   
\<%= post.testo %\>

\<%= link\_to 'Commenti(n)', :action =\> 'show\_comment', :id =\> post %\>\<%= link\_to 'Aggiungi un commento', :action =\> 'add\_comment', :id =\> post %\>
 \<% for comment in @comments %\> \<% if comment.post\_id == post.id %\> 

\<%= comment.testo %\>

 \<% end %\> \<% end %\> \<% if comment.post\_id == post.id %\> \<% form\_for :comment, :url =\> {:action =\> :save\_comment} do |form| %\> 

Nome:  
 \<%= form.text\_field :autore%\>

Testo:  
 \<%= form.text\_area :testo %\>

 \<%= form.hidden\_field :post\_id, :value =\> post.id %\> \<%= submit\_tag "Invia commento" , :class =\> "submit" %\> \<% end %\> \<% end %\> 

```
  </div> <!-- post -->
  <% end %>

```

So che Ã¨ un po’ lungo ma dov’Ã¨ l’errore? Come si puÃ² vedere il form  
chiama il metodo save\_comment, ma se lascio i campi vuoti non mi  
notifica nulla, carica semplicemente la pagina add\_comment…

---

<div class="post-metadata">

**Author:** ![mckracken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5daacb/32.png) [@mckracken](https://www.ruby-forum.com/u/mckracken)\
**Post date:** [April 16, 2008, 11:28pm UTC](https://www.ruby-forum.com/t/problema-con-error-messages-for/134538/2 "2008-04-16T23:28:27Z")

</div>

Emanuele B. wrote:

> ```
> if @comment.save
> flash[:notice] = "Commento inserito con successo"
> redirect_to("/home/show_comment/" +
> 
> ```
> 
> params[:comment][:post\_id])  
> else  
> redirect\_to("/home/add\_comment/" + params[:comment][:post\_id])  
> end

> So che Ã¨ un po’ lungo ma dov’Ã¨ l’errore?

Ã¨ il redirect. se fai un redirect perdi la variabile d’istanza @comment  
e quindi l’array errors associatoci. sostituisci con render :partial =\>  
‘add\_comment’ e sei a posto.
