views:

46

answers:

2

I have a blog model with has many model comments relationship.

So my form looks something like this located in the blog show view:

<% form_for [@blog, @comment] do |f| -%>
  <%= f.error_messages %>

  <%= f.label :message, "Add your message" %>
  <%= f.text_area :message %>

  <%= f.submit "Submit" %>
<% end -%>

All very straightforward.

I have one action in the comment controller and that is a create action.

def create
  @comment = Comment.new(params[:comment])

  respond_to do |format|
    if @comment.save
      flash[:success] = "Thank you for your comment"
      format.html { redirect_to :back }
    else
      format.html { render :action => "new" }
    end
  end
end

My problem is that when a validation error occurs in the comment model then I will end up rendering the new comment view which doesn't exist. I want to render the blog show view along with the error messages. If I try to redirect back or render the blog show view template then the users comments will end up deleted as state is not preserved between them. Can anyone tell me what the conventional solution is to this problem? Thanks.

A: 

KJF,

You'll want the do something like this:

before_filter :fetch_blog

def create
  @comment = @blog.comments.new(params[:comment])
  respond_to do |format|
    if @comment.save
      flash[:success] = "Thank you for your comment"
      format.html { redirect_to blog_path(@blog) }
    else
      format.html { render :controller => 'blog', :action => 'show' }
    end
  end
end

protected
  def fetch_blog
    @blog = Blog.find(params[:blog_id])
  end
Carlos
Thanks, I've tried it that way but the problem I'm having is that the it now renders the path blog/1/commentswhere 1 is the id of the blog post.
KJF
+1  A: 

The solution is to have your blog accept_nested_attributes_for :comments.

By only modifying comments through the blog controller you will ensure that the you return to the blog view with validations if something goes wrong.

Changes you need to make to make it work.

class Blog < ActiveRecord::Base
  ...
  accepts_nested_attributes_for :comments
end

Blog/show view

<% form_for @blog do |f| -%>
  <%= f.error_messages %>
  <% new_comments @blog.comments.select{&:new_record?} %>
  <%= f.fields_for :comments, new_comments.empty? ? @blog.comments.build : new_comments do |c| %>
  <%= c.label :message, "Add your message" %>
  <%= c.text_area :message %>
  <%=end%>

  <%= f.submit "Submit" %>
<% end -%>

If there isn't a update method in your Blog controller you will need to add on to make it work. But I believe this isn't necessary with restful routes as rails will take the common action. However, that might also require you to have a blog edit view that is nearly identical to the show view. Instead use this code in your controller.

class BlogsController < ApplicationController
  def update
    @blog = Blog.find(params[:id])
    if @blog.update_attributes(params[:blog])
      redirect_to @blog
    else
      render :action => show
    end
  end
end

If you're using attr_accessible for the Blog model you will have to add :comments to the list.

EmFi