渲染:动作和渲染:模板之间的区别

发布于 2024-10-18 05:51:36 字数 172 浏览 8 评论 0原文

render :action => 和有什么区别? “new” 和 render :template => “用户/新”?我听说渲染模板可以用于其他控制器的视图。是这样还是两者之间的渲染布局有什么区别吗?对于 render :template,是否需要定义一个操作,或者视图页面本身就足够了?

What is the difference between render :action => "new" and render :template => "users/new"? I have heard that rendering template, we can use for views from other controllers. Is that it or is there any difference in rendering layout also between the two? For render :template, is it neccessary to have an action defined or is the view page itself enough?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

謌踐踏愛綪 2024-10-25 05:51:36

没有区别。
render :template => 'some/thing'render 'some/thing' 相同,也与 render :action =>; 相同。 'thing' 如果我们在 some 控制器中。

来自 Ruby On Rails 指南

render :edit
render :action => :edit
render 'edit'
render 'edit.html.erb'
render :action => 'edit'
render :action => 'edit.html.erb'
render 'books/edit'
render 'books/edit.html.erb'
render :template => 'books/edit'
render :template => 'books/edit.html.erb'
render '/path/to/rails/app/views/books/edit'
render '/path/to/rails/app/views/books/edit.html.erb'
render :file => '/path/to/rails/app/views/books/edit'
render :file => '/path/to/rails/app/views/books/edit.html.erb'

There is no difference.
render :template => 'some/thing' is the same as just render 'some/thing', as well as the same as render :action => 'thing' if we are in the some controller.

From Ruby On Rails guide;

render :edit
render :action => :edit
render 'edit'
render 'edit.html.erb'
render :action => 'edit'
render :action => 'edit.html.erb'
render 'books/edit'
render 'books/edit.html.erb'
render :template => 'books/edit'
render :template => 'books/edit.html.erb'
render '/path/to/rails/app/views/books/edit'
render '/path/to/rails/app/views/books/edit.html.erb'
render :file => '/path/to/rails/app/views/books/edit'
render :file => '/path/to/rails/app/views/books/edit.html.erb'
画▽骨i 2024-10-25 05:51:36

以前,在控制器操作中调用渲染“foo/bar”相当于渲染文件:“foo/bar”。在 Rails 4.2 中,这已更改为 render template: "foo/bar"。如果您需要渲染文件,请更改代码以使用显式形式(渲染文件:“foo/bar”)。

http://guides.rubyonrails.org/4_2_release_notes.html#render -带有字符串参数

Previously, calling render "foo/bar" in a controller action was equivalent to render file: "foo/bar". In Rails 4.2, this has been changed to mean render template: "foo/bar" instead. If you need to render a file, please change your code to use the explicit form (render file: "foo/bar") instead.

http://guides.rubyonrails.org/4_2_release_notes.html#render-with-a-string-argument

你与昨日 2024-10-25 05:51:36
 render :action => 'some_controller_action', :layout => 'some_layout_in_layout_folder'

将渲染该控制器操作的视图并应用 some_layout_in_layout_folder 布局的资源设置(javascriptCSS)。

如果您在控制器操作中编写此内容,它将使用指定布局的样式以及类开头定义的任何布局(如果有)

 render :action => 'some_controller_action', :layout => 'some_layout_in_layout_folder'

will render the view of that controller action and apply the asset settings (javascript, CSS) of the some_layout_in_layout_folder layout.

If you write this in a controller action, it will use the styling of the specified layout in conjunction with any layout defined at the beginning of the class, if any

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文