Rails: use jbuilder template in various management methods

Is it possible to reuse the jbuilder template in another controller method? In other words: how to explicitly specify a controller method to use a specific jbuilder template?

+6
source share
2 answers

From the Rails guide .

Providing an action template from another controller.

What if you want to display a template from a completely different controller from the one that contains the action code? You can also do that with a render that takes the full path (relative to app / views) of the template to render. For example, if you use the code in the AdminProductsController that lives in the application / controllers / admin, you can visualize the results of the action of the template in applications / views / products this way

 def my_action # some code here render "products/show" end 
+9
source
 def my_action # some code here render "products/show.json" end 

or

 def my_action # some code here render "show.json" end 

without .json it will try to display the html file.

+2
source

All Articles