Greg
2012-May-17 18:59 UTC
Partial with nested model/controller render back to current post and show errors
Hi all, I get stucked with the following scenario. I have a simple application where I have a post which can have a lot of comments. In the show action of the post I loaded the partial form to create the comment. This is also working. My problem is when the validation for the comment fails I don''t know how to set the render method in the create action of the comments controller to get back to the current post show view and also to display the form errors. I can do a redirect back to the current post but then I will miss all the form data and see also no errors. Hope someone can give me a hint how to solve this. Here are my models: class Post < ActiveRecord::Base extend FriendlyId friendly_id :title, use: :slugged # attr_accessible :title, :body attr_accessible :title, :teaser, :content, :category_id, :picture #paperclip settings has_attached_file :picture, :styles => { :medium => "300x225>", :small => "180x115>", :thumb => "150x150>" } #association belongs_to :category has_many :comments end class Comment < ActiveRecord::Base attr_accessible :content, :email, :name, :website validates :content, :email, :name, :website, :presence => {:message => "Darf nicht leer sein!"} belongs_to :post end My routes are set like this: resources :posts do resources :comments end Show view post: ... <div class="comment"> <h2>Comments</h2> <br/> show here partial with comments <h2 class="new">New Comment</h2> <%= render "comments/form" %> </div> … Comments Form Partial: <%= form_for([@post,@post.comments.build]) do |f| %> <% if @post.comments.build.errors.any? %> <div id="formErrors"> <h4><%= @post.comments.build.errors.count %> Error while saving form:</h4> <ul> <% @post.comments.build.errors.full_messages.each do |msg| %> <li><%= msg %></li> <% end %> </ul> </div> <% end %> <p> <%= f.label :name %><br/> <%= f.text_field :name %><br/> </p> <p> <%= f.label :email %><br/> <%= f.text_field :email %><br/> </p> <p> <%= f.label :website%><br/> <%= f.text_field :website %><br/> </p> <p> <%= f.label :content %><br/> <%= f.text_area :content %><br/> </p> <%= f.submit "Save" %> <% end %> create action in comments controller def create @post = Post.find(params[:post_id]) @comment = @post.comments.build(params[:comment]) respond_to do |format| if @comment.save format.html { redirect_to(post_path(@post), :notice => "Comment successfully created") } else flash[:alert] = "Something went wrong!" format.html { render ???? } end end end Thanks for any help with this. Cheers Greg -- 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.
Greg
2012-May-17 20:13 UTC
Re: Partial with nested model/controller render back to current post and show errors
Well, I should have read more on the rails guides website. I found the solution so easy and simple. def create @post = Post.find(params[:post_id]) @comment = @post.comments.build(params[:comment]) respond_to do |format| if @comment.save format.html { redirect_to(post_path(@post), :notice => "Comment successfully created") } else flash[:alert] = "Something went wrong!" format.html { render ''posts/show'' } end end end And also changed this in my partial: <% if @comment.errors.any? %> <div id="formErrors"> <h4><%= @comment.errors.count %> Error while saving form:</h4> <ul> <% @comment.errors.full_messages.each do |msg| %> <li><%= msg %></li> <% end %> </ul> </div> <% end %> Problem solved! Cheers Greg Am 17.05.2012 um 20:59 schrieb Greg:> Hi all, > I get stucked with the following scenario. I have a simple application where I have a post which can have a lot of comments. In the show action of the post I loaded the partial form to create the comment. This is also working. My problem is when the validation for the comment fails I don''t know how to set the render method in the create action of the comments controller to get back to the current post show view and also to display the form errors. I can do a redirect back to the current post but then I will miss all the form data and see also no errors. Hope someone can give me a hint how to solve this. > > Here are my models: > > class Post < ActiveRecord::Base > extend FriendlyId > friendly_id :title, use: :slugged > # attr_accessible :title, :body > attr_accessible :title, :teaser, :content, :category_id, :picture > > #paperclip settings > has_attached_file :picture, :styles => { :medium => "300x225>", :small => "180x115>", :thumb => "150x150>" } > > #association > belongs_to :category > has_many :comments > end > > class Comment < ActiveRecord::Base > attr_accessible :content, :email, :name, :website > > validates :content, :email, :name, :website, :presence => {:message => "Darf nicht leer sein!"} > > belongs_to :post > end > > > My routes are set like this: > > resources :posts do > resources :comments > end > > Show view post: > ... > <div class="comment"> > <h2>Comments</h2> > <br/> > show here partial with comments > > <h2 class="new">New Comment</h2> > <%= render "comments/form" %> > </div> > … > > Comments Form Partial: > > <%= form_for([@post,@post.comments.build]) do |f| %> > <% if @post.comments.build.errors.any? %> > <div id="formErrors"> > <h4><%= @post.comments.build.errors.count %> Error while saving form:</h4> > <ul> > <% @post.comments.build.errors.full_messages.each do |msg| %> > <li><%= msg %></li> > <% end %> > </ul> > </div> > <% end %> > <p> > <%= f.label :name %><br/> > <%= f.text_field :name %><br/> > </p> > <p> > <%= f.label :email %><br/> > <%= f.text_field :email %><br/> > </p> > > <p> > <%= f.label :website%><br/> > <%= f.text_field :website %><br/> > </p> > > <p> > <%= f.label :content %><br/> > <%= f.text_area :content %><br/> > </p> > > <%= f.submit "Save" %> > > <% end %> > > create action in comments controller > > def create > @post = Post.find(params[:post_id]) > @comment = @post.comments.build(params[:comment]) > respond_to do |format| > if @comment.save > format.html { redirect_to(post_path(@post), :notice => "Comment successfully created") } > else > flash[:alert] = "Something went wrong!" > format.html { render ???? } > end > > end > end > > > > Thanks for any help with this. > > Cheers Greg > > -- > 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.-- 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.