Daniel Brockman
Daniel Brockman

Reputation: 19290

How do I find the file name of a Rails partial?

In Rails 3, how do I find the file name of the partial that would be rendered by render 'foo'?

Upvotes: 2

Views: 1043

Answers (1)

mccannf
mccannf

Reputation: 16659

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.

In a controller bar calling render 'foo' would result in rendering the file <project>/app/views/bar/foo.html.erb.

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.

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

http://guides.rubyonrails.org/layouts_and_rendering.html#using-partials

Upvotes: 4

Related Questions