silent
silent

Reputation: 45

Rails 3: How to render an action from another controller's view

I'm trying to call an action from a different controller's view:

Controller Countries has an action called selectbox which generates a html selectbox from all countries as a partial.
Controller Customers has an action called new which generates a html form for all the customers attributes. I want to insert the selectbox from Countries#selectbox in this form.

What would be the right way to achieve this?

Upvotes: 3

Views: 2896

Answers (2)

Swanand
Swanand

Reputation: 12426

For what you are doing, extracting the code into helper method is the right way to do it. However, if you still want to use am action from another, this is the official Rails plugin you can use:

https://github.com/rails/render_component

Upvotes: 1

Miguel Ping
Miguel Ping

Reputation: 18347

You're doing it wrong. If there's a piece of code that is to be reused (such as html selectbox generation), you should put it in a helper and/or use a partial to render the html selectbox part.

Bear in mind this is only good advice if the code is somewhat complex (say, more than one or two lines). Here's a post I found while googling that may help you: helper or partial

Upvotes: 2

Related Questions