简体   繁体   English

在路线中渲染RESTful路线

[英]render in routes RESTful routes

I'm not exactly sure if I have a clear understanding what rendering means in RESTful routes. 我不确定我是否清楚了解RESTful路由中的渲染意味着什么。

For example: In my Pages controller, 例如:在我的Pages控制器中,

class PagesController < ApplicationController
  def home
    render "home.html.erb"
  end
end

on my routes.rb file i have the following: 在我的routes.rb文件中,我具有以下内容:

get "/" => "pages#home"

does render home.html.erb mean output the info on this page? render home.html.erb意味着在此页面上输出信息吗?

Thanks! 谢谢!

Yes. 是。 render does the work of rendering your application's content for use by a browser when your action is invoked. 当调用您的操作时, render会完成渲染应用程序内容以供浏览器使用的工作。 You don't really need to explicitly specify the name of the view if your view name matches the action name and placed in the right folder in app/views 如果您的视图名称与操作名称匹配并且放置在app / views的正确文件夹中,则实际上不需要显式指定视图的名称。

for eg, if you have your view in app/views/pages/ , your controller can just be 例如,如果您在app/views/pages/拥有视图,则您的控制器可以是

class PagesController < ApplicationController
  def home
  end
end

And even if you want to render a template which name is different than the action name (or localized in another place); 并且即使您要渲染名称与动作名称不同(或本地化在另一个地方)的模板; you don't need to specify the file extension, only its name (path/name if outside the scope of the designated folder for the views of your controller)... 您无需指定文件扩展名,只需指定其名称(如果在控制器视图的指定文件夹范围之外,则为路径/名称)...

Ex, if you have a template app/views/pages/home_template.html.erb for your home action you could do 例如,如果您有用于home操作的模板app/views/pages/home_template.html.erb ,则可以

class PagesController < ApplicationController
  def home
    render 'home_template' 
  end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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