Ciao,
ho una action ‘show’ di un controller ‘Posts’ con questo form
<% form_for([@post,Comment.new]) do |f| %>
<%= f.error_messages %>
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.label :testo %>
<%= f.text_area :testo %>
<%= f.submit "Aggiungi commento" %>
<% end %>Il form reindirizza alla funzione ‘create’ di un controller ‘Comments’:
def create
@post = Post.find(params[:post_id])
@comment = @post.comments.new(params[:comment])
respond_to do |format|
if @comment.save
flash[:notice] = ‘Commento inviato con successo’
format.html { redirect_to @post }
format.xml { render :xml => @comment, :status => :created,
:location => @comment }
else
format.html { render :template => “posts/show”, :params => { :id
=> @post.id} }
format.xml { render :xml => @comment.errors, :status =>
:unprocessable_entity }
end
end
end
Il form funziona se compilo i campi; se non metto nulla, entrano in
azione i validates_presence_of del model Comment, ma non vengono
stampati a video gli errori in <%= f.error_messages %>.
Mi sono dimenticato qualcosa?
Saluti
Alberto