pbeshai
2010-Oct-30 20:32 UTC
Error messages for nested resource; guide says to use f.error_messages, but is deprecated/didn''t work.
I''ve been trying to set up a new Rails 3 project as per http://guides.rubyonrails.org/getting_started.html, but I''m running into difficulty with displaying errors when adding a new comment. The comments are a nested resource of posts. On the posts/show page, I have <%= form_for([@post, @post.comments.build]) do |f| %> <%= f.error_messages %> <div class="field"> <%= f.label :commenter %><br /> <%= f.text_field :commenter %> </div> <div class="field"> <%= f.label :body %><br /> <%= f.text_area :body %> </div> <div class="actions"> <%= f.submit %> </div> <% end %> But the f.error_messages is deprecated. I''ve read online that one solution is to use a partial and pass in the offending object as a target to it, but I don''t know how to reference the comments object. It would look something like <%= form_for([@post, @post.comments.build]) do |f| %> <%= render"shared/error_messages", :target => @comment %> … <% end %> where @comment is the comment object. The examples online don''t show how to do it when you pass an array to form_for. How can I get the error messages to show up for new comments? For reference, the CommentsController looks like: class CommentsController < ApplicationController def create @post = Post.find(params[:post_id]) @comment = @post.comments.create(params[:comment]) redirect_to post_path(@post) end end and the Comment model looks like: class Comment < ActiveRecord::Base validates :commenter, :presence => true validates :body, :presence => true belongs_to :post end -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Arailsdemo A.
2010-Oct-31 03:26 UTC
Re: Error messages for nested resource; guide says to use f.error_messages, but is deprecated/didn''t
You want to set your comment variable in the controller: def new @comment = @post.comments.build end so your form is <%= form_for([@post, @comment]) do |f| %> <%= render"shared/error_messages", :target => @comment %> <% end %> If you build the comment in the form (which you shouldn''t be doing anyways if you''re sticking to MVC principles), then when you render the form, a new comment object is always created. Since it''s a new object, it won''t have any errors. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.