I'm not sure if this is convention or not, but I have been working through Hartl's Rails tutorial and I noticed that he creates all his partial files with a leading underscore. For example _user.html.erb. Is this something that is necessary for the partial to work, or is this a stylistic choice?
It's necessary for partials to work. From the official Rails guide:
3.4.1 Naming Partials
To render a partial as part of a view, you use the render method within the view:
<%= render "menu" %>
This will render a file named _menu.html.erb at that point within the view being rendered. Note the leading underscore character: partials are named with a leading underscore to distinguish them from regular views, even though they are referred to without the underscore. This holds true even when you're pulling in a partial from another folder:
Related
I was reading code of linuxfr.org, a rails open source project. I am surprised that the following code from application.html.haml does not have a "yield" statement (without any symbol associated with it), so how will the content be rendered with the layout?
https://github.com/linuxfrorg/linuxfr.org/blob/master/app/views/layouts/application.html.haml
For example, how does the layout merge with the show view file?
https://github.com/linuxfrorg/linuxfr.org/blob/master/app/views/wiki_pages/show.html.haml
By default, yield is the same as yield :layout. Rails assumes that all code inside view files are content_for :layout if not specified otherwise.
I understand that .html.erb files go within app/views or its subfolders. But what is the app/views/layouts folder in particular for in Rails?
app/views/layouts is the folder in which rails looks for the layouts.
From http://guides.rubyonrails.org/layouts_and_rendering.html#finding-layouts :
To find the current layout, Rails first looks for a file in
app/views/layouts with the same base name as the controller. For
example, rendering actions from the PhotosController class will use
app/views/layouts/photos.html.erb (or
app/views/layouts/photos.builder). If there is no such
controller-specific layout, Rails will use
app/views/layouts/application.html.erb or
app/views/layouts/application.builder. If there is no .erb layout,
Rails will use a .builder layout if one exists. Rails also provides
several ways to more precisely assign specific layouts to individual
controllers and actions.
What is a layout?
A layout defines the surroundings of an HTML page. It's the place to define common look and feel of the page.
The RailsCasts episode - All About Layouts - though very old, is still very useful in this context.
Layout in rails framework is very important folder, main layout of your application is define here as application.html.erb and all the views are yielded here using <% yield %>
Ok, I realise this question has been asked many times but the answers never seems to address the issue/question I have with this.
I have a js file that I would like to include on specific pages only. There are many responses that suggest that I put files into folders and then in the manifest file reference only those folders - for example this Railscast (at about 06:20) talks about this.
However, I only have one application layout file (and I guess this could be the area I'm lacking in) - therefore this file points to the application.js manifest and therefore I can't see how I can include things conditionally.
It's a bit like this resource too - http://railsapps.github.io/rails-javascript-include-external.html - scroll down to the page-specific scripts sub heading and it repeats what the Railscast suggests. But nothing is mentioned of multiple application layout files.
If anyone can help me clarify what to do in this situation I would be most grateful.
I should perhaps point out that I'm using Rails 4.
You can use content_for in your views to "inject" content into the layout when said view is to be rendered. See: using-the-content-for-method
You'd need to do a few things to make this happen:
Add the placeholder to yield the content in the layout. ex.
<%= yield :js %>
Add the block (to be yielded) to your view. ex:
<%= content_for :js do %>
<%= javascript_include_tag "my_script" %>
<% end %>
If you are using the asset pipeline in production and you want to reference a particular asset like a "my_script.js", in your production.rb or relevant environment config.you will need to precompile it using:
config.assets.precompile=["my_script.js"]
In application.html.erb, use this to add controller specific javascript instead of application specific javascript. Make sure you remove require_tree.
<%= javascript_include_tag params[:controller] %>
Read more on this topic on Rails Guide.
If I have multiple views, but they are supposed to share the same partial (Footer and ad bars for example), where should these shared partials go?
I would create a shared folder in views and put all my shared partials in it. You can call the partial like this:
<%= render 'shared/partialname' %>
While creating a partial you have to put underscore infront of the name of file.
like footer inside a layout folder -> _footer.html.erb
Then you have to specify at particular location by using this statement :
<%= render 'layout/footer' %>
Is it possible to define a .js.erb file as a partial? I have several .js.erb that have code that could just be moved to a common file like a partial, and then I would just call render on it.
How can I do this?
Same rules as normal partials: name the file beginning with _yourfile.js.erb and in your main file call it with <%= render partial: "yourjsfile" %>
Although, being javascript a programming language, you might want to think to refactor your code instead of resorting to this