rails: use jbuilder template in various controller methods

11,089

Solution 1

From Rails guide.

Rendering an Action's Template from Another Controller.

What if you want to render a template from an entirely different controller from the one that contains the action code? You can also do that with render, which accepts the full path (relative to app/views) of the template to render. For example, if you're running code in an AdminProductsController that lives in app/controllers/admin, you can render the results of an action to a template in app/views/products this way

def my_action
  # some code here
  render "products/show"
end

Solution 2

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

or

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

without the .json it will try to render an html file.

Share:
11,089

Related videos on Youtube

meteor
Author by

meteor

Updated on September 15, 2022

Comments

  • meteor
    meteor over 1 year

    Is it possible to reuse jbuilder-template in another controller method? In other words: how to explicitly say controller method to use concrete jbuilder-template?

  • meteor
    meteor almost 9 years
    didn't think that this propagates to jbuilder, thank you green man