In Rails 3, how do I find the file name of the partial that would be rendered by render 'foo'
?
在Rails 3中,如何找到渲染'foo'渲染的局部文件名?
1 个解决方案
#1
3
Just to be clear, you get two different results if you call render
from within a controller, and render
from within tags in a view.
为了清楚起见,如果从控制器中调用render,并从视图中的标记内进行渲染,则会得到两个不同的结果。
In a controller bar
calling render 'foo'
would result in rendering the file <project>/app/views/bar/foo.html.erb
.
在调用render'foo'的控制器栏中,将导致呈现文件
In a view e.g. <project>/app/views/bar/show.html.erb
calling <% render 'foo' %>
would result in including the partial <project>/app/views/bar/_foo.html.erb
into the show.html.erb
view. The partial is taken from the same directory as the view.
在视图中,例如
And to make things a little more complex, if you put folder names into the render e.g. render 'shared/foo'
this is relative to <project>/app/views
.
如果您将文件夹名称放入渲染中,例如将事情变得更复杂一些,例如渲染'shared / foo'这是相对于
So:
Controller: render 'foo' = <project>/app/views/<controller name>/foo.html.erb
render 'shared/foo' = <project>/app/views/shared/foo.html.erb
View: <% render 'foo' %> = _foo.html.erb in directory of view
<% render 'shared/foo' %> = <project>/app/views/shared/_foo.html.erb
P.S. if you specify a different :format
in your render
command, then the html.erb
needs to change accordingly (e.g. js.erb
, js.coffee.erb
, etc etc).
附:如果你在render命令中指定了另一种:format,那么html.erb需要相应地改变(例如js.erb,js.coffee.erb等)。
http://guides.rubyonrails.org/layouts_and_rendering.html#using-partials
#1
3
Just to be clear, you get two different results if you call render
from within a controller, and render
from within tags in a view.
为了清楚起见,如果从控制器中调用render,并从视图中的标记内进行渲染,则会得到两个不同的结果。
In a controller bar
calling render 'foo'
would result in rendering the file <project>/app/views/bar/foo.html.erb
.
在调用render'foo'的控制器栏中,将导致呈现文件
In a view e.g. <project>/app/views/bar/show.html.erb
calling <% render 'foo' %>
would result in including the partial <project>/app/views/bar/_foo.html.erb
into the show.html.erb
view. The partial is taken from the same directory as the view.
在视图中,例如
And to make things a little more complex, if you put folder names into the render e.g. render 'shared/foo'
this is relative to <project>/app/views
.
如果您将文件夹名称放入渲染中,例如将事情变得更复杂一些,例如渲染'shared / foo'这是相对于
So:
Controller: render 'foo' = <project>/app/views/<controller name>/foo.html.erb
render 'shared/foo' = <project>/app/views/shared/foo.html.erb
View: <% render 'foo' %> = _foo.html.erb in directory of view
<% render 'shared/foo' %> = <project>/app/views/shared/_foo.html.erb
P.S. if you specify a different :format
in your render
command, then the html.erb
needs to change accordingly (e.g. js.erb
, js.coffee.erb
, etc etc).
附:如果你在render命令中指定了另一种:format,那么html.erb需要相应地改变(例如js.erb,js.coffee.erb等)。
http://guides.rubyonrails.org/layouts_and_rendering.html#using-partials