1

In my Rails app, I have a Foo model that has_many Bars. I want to edit the Bars inside the show view of Foo's controller. Also, I don't want to reload the entire page each time I edit a Bar.

To do this, I'm using AJAX. The Bar controller has two views - show and edit. bar#show has an edit button, and both the Bar views, instead of reloading the entire page, use AJAX to perform the HTTP request and then replace the content of the HTML tag that contains that represents that specific Bar object in the Foo's show view.

When rendering foo#show, I don't want to perform an HTTP request for each Bar object. Also, Bar's controller method need to fetch the Bar record from the database - something I don't want to do in foo#show, because I can load all of them with a single query, which is much faster than loading each one separately.

So, what I did is to make Bar's views partials instead of normal views. That way, I can render them both from the controller and from another view.

Now, the problem is that those views need more preparation beside loading the Bar record from the database, and unlike loading the record - those preparations need to be done whether the partial is rendered from the controller or from another view.

What I'm doing now is having a ruby block of about 10 lines in the beginning of the partial's erb, and do the preparation there. While that works, I realize it's a bad design - that kind of preparation should be done in controllers, not in views. And to make things worse - sometimes, that preparation code of the show partial can decide that the edit partial needs to be rendered instead, so I need to call another partial render from the show partial - again, something that should have been done in another place.

However, I just don't know where to put that code. I thought about putting it a view helper, but view helpers are supposed to be called from views, and I also need to call this one from a controller.

So, what's the RoR way to solve this problem?

1 Answer 1

1

Why not create a controller method? Remember, controller methods do NOT have to be bound to actions - controller is just another class in the app. This controller method can set a state (variable in the controller) that can be used in your partials. Also, action methods of said controller can be implemented in terms of that method (i.e. call it to get/prepare the data they need).

class MyController < ApplicationController
  def show
    @intermediate_state = another_method
  end

  private
  def another_method
    "some value is returned here!"
  end
end

View/Partial:

<%= @intermediate_state %>
4
  • So the controller method is called from the partial, or do I call the controller method to render the partial?
    – Idan Arye
    Apr 11, 2013 at 1:20
  • You call controller method from action method (also on controller) and then use the state to render the partial. See example in edited answer. Apr 11, 2013 at 1:55
  • I don't see how this helps when I render the partial from a view in another controller...
    – Idan Arye
    Apr 11, 2013 at 9:53
  • 1
    If you want to do the same in another controller, you can either make another_method static, so that another controller can call MyController.another_method and get the data it needs or you can move it to the lib/ folder in a new class, and use that class in both controllers. Either way, your code will be DRY Apr 11, 2013 at 18:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.