Integrate ruby variable into a js - ruby-on-rails

How I can integrate ruby variable into a js file. I would like to render a partial relative to the current step of registration of my User.
But I can't succeed it, Rails do not translate #flat.current_step in his value (which is a integer). Any ideas ?
$('.flats').replaceWith("<%= j render partial: 'flats/steps/step#{#flat.current_step}' %>");
Error
ActionView::Template::Error (Missing partial flats/steps/_step#{#flat.current_step}

Ruby does not interpolate the #{} when wrapped in single quotes. Change your call to:
$('.flats').replaceWith('<%= j render partial: "flats/steps/step#{#flat.current_step}" %>');

Related

How do i post a form in rails using ajax?

I have a rails app that I need to use Ajax to post on my model.
I have only this line in my filename.js.erb
$('#new-band a').html('<%= j render 'form'%>')
Here is my error:
Showing /<snip>/ajax/app/views/layouts/application.html.erb where line #8 raised:
undefined method `render' for #<#<Class:0x00007fad63eb3b98>:0x00007fad5e2c13f8>
Try changing your single quote to double quotes around the form. For example:
$('#new-band a').html('<%= j render 'form'%>')
Changes to:
$('#new-band a').html('<%= j render "form" %>')
You can try this:
$('#new-band a').html("<%= j render(:partial => 'form') %>");

Rendering a partial via coffeescript prints literal

I'm trying to render a partial from coffeescript when a button is clicked, so I have one of my coffeescript files "script.js.coffee" with the following code:
$('[id^="btn_add_rep_dia_"]').click ->
id = this.id.substring(16)
$("#dia" + id).append("<%= j render('repeticions/form') %>")
But what's happenning is that at the end of $("#dia" + id) appears litterally the text "<%= j render('repeticions/form') %>". The partial is located in "repeticions" folder and it's called "_form.html.haml"
I have also tested the last code line as:
$("#dia" + id).append("<%= escape_javascript( render partial: 'repeticions/form') %>")
You are trying to execute an erb (embedded ruby) template inside a .coffee file. Rails doesn't know that it should pre-process the file before sending it to the client.
If you are using a Rails version > 3.2, then simply renaming your coffeescript file from script.js.coffee to script.js.coffee.erb should be enough to instruct rails to interpret the erb style template strings (<%...%>).
You can't render a partial from a plain coffeescript file. If you wanted to do this approach, I suggest turning that button into a link_to with remote: true which will enable you to use ajax in rails.

Rails: Calling Render From Within a Generator

I've just started experimenting with generators. In one of my generated view templates, I want to call render like so:
index.js.slim
transition("#main", "<%= escape_javascript(render 'index') %>");
When I try using the generator, I get this error:
(erb):1:in `template': undefined method `render' for #<Slim::Generators::ScaffoldGenerator:0x000000041b2a20> (NoMethodError)
Is Rails really incapable of calling render from within a generator? Or am I doing something wrong?
Railscast 218 goes into more detail:
The first thing to note is that because we’re using the template
method, all of the erb tags in the code will be executed when the
generator runs. If we want to include any erb in the generated file
we’ll have to escape the percent sign at the beginning of each erb tag
and we’ve done that for most of the erb code above.
In this instance:
transition("#main", "<%= escape_javascript(render 'index') %>");
should become:
transition("#main", "<%%= escape_javascript(render 'index') %>");
All it needed was an extra % to escape the erb.

Rails impossible to render partials

I'm using the gems "haml" and "haml-rails" in my rails app and I have this folder structure
-views
-layouts
-public
-layout.html.haml
-_header.html.haml
-_footer.html.haml
And i wanto to render _header and _footer in layout.html.haml using this code:
= render 'layouts/public/_header'
.container= yield
= render 'layouts/public/_footer'
but rails raises a MissingTemplate error but _header and _footer exists...
how can i solve?
You typically omit the underscores when specifying partial names in these helpers. Also, you should be passing them in as a :partial parameter:.
= render :partial => 'layouts/public/header'
.container= yield
= render :partial => 'layouts/public/footer'
partials are named with a leading underscore to distinguish them from
regular views, even though they are referred to without the
underscore.
source: Rails Guides

Can HAML do a "capture", kind of like a render_to_string in Ruby on Rails?

I heard that HAML has a capture function that can do something like Ruby on Rails's render_to_string, but can't find info on it. Actually, in View, we can use aString = render :partial ... and render actually works the same as render_to_string (as on Rail 2.2.2). But is there also an HAML way of doing it by capture?
Yes, you can capture the Haml buffer with capture_haml. You have to include Haml::Helpers in order to use it.
However, I am not sure if it works for capturing partials. From my understanding, I'd say that Haml is independent from render and thus, render_to_string or render :partial should also work for Haml.
At least, the following will work:
str = capture_haml do
haml_tag "p#feedback.success", "Your request has been successful."
end
str # => "<p id='feedback' class='success'>Your request has been successful.</p>"

Resources