Rails 4: using a js.erb file as a partial - ruby-on-rails

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

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?

CoffeeScript in Rails does not correctly render partial

$('#some').append('<%= render 'my_partial' %>')
And it renders it as text. It always escapes html. I have tried in all ways to avoid escaping.
$('#some').append('<%= j render('my_partial') %>')
$('#some').append('<%= raw render('my_partial') %>')
Result is the same.
If you want your coffee (or any files) to be interpreted as ERB, then you need to append the file name with .erb ie app.coffee.erb.
But a better question is why? Doing so will tightly couple your coffee to both rails and ruby making re-use much harder - much like putting css inline in HTML.
There are two better options IMO:
Use data attributes in HTML and access them with javascript
If you only have a single variable to pass, try something like <%= javascript_tag "my_func(#{#my_var})" %>. This will invoke a javascript function with that variable.

Where should shared partials go?

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

how to share code between Unobtrusive JavaScript in rails

As the title, I want to abstract some function and share them between different unobtrusive javascript files. How can I do?
To be more specific, for example, I have two files: show.js.erb, create.js.erb in views/object/
When I response to ajax request, I will render them.
Now this two files share some same code, so I want to abstract them and put it to a new file.
How can I achieve this?
Like with html.erb files you can also have partial for js.erb files.
Just extract the common code into a partial (starting with a _, so e.g _common_code.js.erb in views/object/).
Then you can just use the render function in the show.js.erb or create.js.erb files like this to include your common code.
render "common_code"
try extract common logic to separate file, include it to layout and use common functions from *.js.erb files.

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.

Resources