Where should shared partials go? - ruby-on-rails

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' %>

Related

How do I render an SVG inline in Rails?

I have an SVG file that's part of a template located at:
vendor/theme/assets/icons/icon-1.svg
How do I render that inline in my view? render partial: path fails and says it can't find a partial.
In your view insert the following:
<%= render inline: Rails.root.join('vendor/theme/assets/icons/icon-1.svg').read %>
If you're going to be doing this multiple times, you may want to refactor this into a helper.
Also, think about the following:
Are you okay with dumping third party code directly into your view?
Is the vendor SVG file updated automatically without review?
Are you sure the vendor SVG file will never contain malicious code?

Why do partials in Rails start with an underscore?

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:

linking to html file in /public

I have a file 'maps.html' located in /public. I am loading up an index page with contents as follows:
<%= link_to 'redirect_click_here', 'maps.html' %>.
Application.html.erb takes care of the other necessary html elements for the page.
The result of clicking that link is that I am sent to /map/maps.html.
This is slightly logical: the page hosting the link was in the 'map' controller. Still, I want to 'escape' the controller and access the public html file.
I realize that this is a kind of pointless request because I could just put the html file in app/views, but it's just for completion's sake that I put forth this request.
edit
One reason I want to include this file from the /public/ directory is that I don't want it to go through the asset pipeline and inherit the html document structure from application.html.erb. I am going to be including HTML files which include custom heads and I don't want to have to replace the contents of application.html.erb every time.
You should use '/maps.html' in your link, so that it knows it is in the root public folder.
You can access it by
<%= link_to "Maps", "/your_project/maps.html" %>

Ruby questions about web layer layout

I am trying to make a URL like Link Title from within my application. What is the file that I need to edit to link this URL to a controller and then a view?
Also, I am still working my way through this tutorial:
http://guides.rubyonrails.org/layouts_and_rendering.html
and I am wondering when they do something like this:
<%= stylesheet_link_tag "http://example.com/main.css" %>
is that supposed to live in the application.html.erb file or the index.html.erb file?
What is the file that I need to edit to link this url to a controller and then a view?
routes.rb
See the rails guide
is that supposed to live in the application.html.erb file or the index.html.erb file?
The simple answer is: application.html.erb, inside the head section. There are ways of injecting view template stuff into the head, but if you're just starting out, stick with application.html.erb.

Rails 4: using a js.erb file as a partial

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

Resources