Ruby-on-rails 渲染:动作和渲染:模板之间的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5045222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between render :action and render :template
提问by rubyprince
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?
render :action => "new"和 和有render :template => "users/new"什么区别?我听说渲染模板,我们可以用于来自其他控制器的视图。是这样还是两者之间的渲染布局也有区别?对于渲染:模板,是否需要定义一个动作或者视图页面本身就足够了?
回答by christianblais
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 somecontroller.
没有区别。render :template => 'some/thing'与 just 相同render 'some/thing',也与render :action => 'thing'我们在some控制器中一样。
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'
回答by user3118220
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.
以前,"foo/bar"在控制器操作中调用 render等效于render file: "foo/bar". 在 Rails 4.2 中,这已改为意思render template: "foo/bar"。如果您需要渲染文件,请更改您的代码以使用显式形式 ( render file: "foo/bar")。
http://guides.rubyonrails.org/4_2_release_notes.html#render-with-a-string-argument
http://guides.rubyonrails.org/4_2_release_notes.html#render-with-a-string-argument

