1

Possible Duplicate:
Rails: call another contoller action from a controller

Hi I'm new to rails and in my home page view I want to call actions in other controllers. I am using render 'the/relevant/url'.

This is calling the view but not hitting the correct controller actions code and therefore not setting up the view models that the views are expecting.

I am coming from asp.net mvc world and here we can use render action and this will hit the controller code for the action.

Can this be done in rails?

Is asp.net doing mvc pattern wrong if you can't?

Many Thanks

UPDATE

My intention is to call from the home page's view another controller actions to get side data not related to the main controllers details, eg. Quote of the day or Hot deal of the day.

Community
  • 1
  • 1
c00ke
  • 2,245
  • 5
  • 26
  • 34
  • This probably goes against the MVC pattern. Can you paste more of your code or explain what you are trying to do from the home page controller? – lebreeze Jan 08 '12 at 09:19

3 Answers3

2

This is considered bad practice. When you are building the view in the controller, a significant portion of the data will be stateful. It is possible to hack together a crazy solution using render and template overrides, but basically, you should never do this.

Instead, use redirect_to other_controller_action_url (which will send a HTTP 302 FOUND by default), or even better, figure out where your code is repeating and factor it out into a helper or presenter.

Edit: if you are trying to get something to stuff in a sidebar like that, you shouldn't be using the entire rails stack to render such a small part of your response. Instead, you would be better off using helpers to generate the specific data and using a partial to render out you sidebar.

locriani
  • 4,995
  • 2
  • 23
  • 26
  • How would you get data which is not related to the main part of the view or controller you are on then? This data does belong to the main view data so don't want to add to the view model. It is side data on the right hand side of the webpage. – c00ke Jan 08 '12 at 12:02
  • Use a partial, or use a flash message. – locriani Jan 08 '12 at 12:13
  • thanks how then does the partial get it's data, I need to get from database or cache? – c00ke Jan 08 '12 at 12:25
  • Yes. If you have transient data that you need to use in a complex manner, you should consider persisting it to the database or to a cache if it is something you precalculate. – locriani Jan 08 '12 at 12:30
1

You will have to use redirect_to, to pass from one controller action to another in rails.

Wahaj Ali
  • 4,093
  • 3
  • 23
  • 35
1

"This is calling the view but not hitting the correct controller actions code and therefore not setting up the view models that the views are expecting."

is very confusing.

I would just read up more using a book or two, like Agile Web Development with Rails and the O'Reilly Ruby book.

Start with the browser page, i.e. view. So a user clicks a link, it calls a controller action, does some stuff. It can call others controllers action with Controller.action to do things. The html it displays will be from the initial controllers render statement, unless it redirects to another controller which does the render.

When you come from asp, php, etc. rails means re-wiring your understanding and it usually takes a while. I came from cold fusion and had to go thru this process.

Michael Durrant
  • 93,410
  • 97
  • 333
  • 497