Topic: Errors in another controller

Hi, I'm quite new to Rails and I'm having a little problem trying to figure out how to do some things the right way in Rails. I'm writing a simple blog application (surprise.).
So I have models and controllers for entries and comments. The comment fields are placed below the entry, so they are shown with controller "entry", and action "show". When errors occur while entering comments, I want to display them below the comment form, on the /entry/show page. But when I simply redirect there from /comment/new, the errors get lost. I also tried a custom error page, that simply includes /entry/show (via render), but that actually rendered nothing. And having the comment-entering stuff in the entry controller doesn't seem to be the right way.
Could anyone point me into the right direction?
Thanks in advance
Kolja

Re: Errors in another controller

you could'nt post some code could you?

--

Re: Errors in another controller

Oh, sorry.
So the comment controller method looks like this:

  def new
    @comment = Comment.new(@params["comment"])
    @entry = Entry.find(@comment.entry_id)
    if @comment.save
      redirect_to :controller => "entry", :action => "show", :id => @entry.to_param
    else
      render "failed"
    end
  end

failed.rhtml would then be
<%= render :controller => "entry", :action => "show", :id => @entry.id %>

What I also tried was to write the comment controller method like this:
  def new
    @comment = Comment.new(@params["comment"])
    @entry = Entry.find(@comment.entry_id)
    @comment.save
    redirect_to :controller => "entry", :action => "show", :id => @entry.to_param
  end

/app/views/entry/show.rhtml has this in it:
      <div class="errors">
        <% if @comment -%>
        <%   unless @comment.errors.empty? -%>
        <%=    "foobar" %>
        <%   end -%>
        <% end -%>
      </div>

So how do I get @comment accessible from there? Or should I do it in a completely different way?
I hope that is enough code, at least it's all I've manipulated trying to get this to work.

Last edited by Kolja (2006-11-27 09:19:18)

Re: Errors in another controller

Doesn't anybody have an idea? I can't be the first one wanting to do something like that..

Re: Errors in another controller

It looks like the only time you are doing a redirect on the create action is when the comment saves successfully, so I'm not certain what the problem is. You may want to try this for rendering:

if @comment.save
  redirect_to :controller => "entry", :action => "show", :id => @entry.to_param
else
  render :action => 'entry/show'
end

Haven't tested rendering an action in another controller before though.

Railscasts - Free Ruby on Rails Screencasts

Re: Errors in another controller

I will try that, but I guess I won't have the data from action "show" in controller "entry" then. What I am trying to do is not having duplicate code or templates, and have everything in the right place.
When I display errors in comment/new, I will have to have the entry fetching code from entry/show a second time in comment/new, and would have to use the same template for both of them (though I guess I could access it like you wrote instead of copying). Or I could put the code for creating comments into some other action in controller "entry", but then, at least in my opinion, that code would be in the wrong place, and I still would have to duplicate the code for getting the other stuff I display on that page from the database.

So basically, I need to have both the instance variables from comment/new and from entry/show accessible in my template. Is there a way to accomplish this?

Last edited by Kolja (2006-11-29 02:34:47)

Re: Errors in another controller

What I would do is add a "new" action to this comments controller with its own form. You can move the comments form into a partial so that code isn't duplicated. You can then render this "new" action with the errors if there is a problem. This way you don't have to worry about resetting the instance variables for the entry/show action.

Railscasts - Free Ruby on Rails Screencasts

Re: Errors in another controller

OK, not as nice or clean as I had imagined, but I guess I'll do that or something similar. So there is no way of calling two different controller methods in different controllers and somehow getting all the data into the template?

Thanks

Re: Errors in another controller

You can move the common code up into a private method in ApplicationController and call it before the template. Another way is to move the custom find logic into the model so you can call it directly from the view - then the template won't rely on any code in the controller. It all depends upon your situation.

Railscasts - Free Ruby on Rails Screencasts

Re: Errors in another controller

What I now did was put the code into the entry controller (it's only two lines anyway), but the views are partials in comment/. It seems to work pretty well, and if I'm lucky I won't have to touch the commenting system again, as everything works and is fairly clean.

Thanks for your help!