简体   繁体   中英

rails: use jbuilder template in various controller methods

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?

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
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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM