Rails: Calling Render From Within a Generator - ruby-on-rails

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.

Related

Integrate ruby variable into a js

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}" %>');

Can't render a rails partial inside CoffeScript file

I try something like this on CoffeScript file located the assets pipeline.
<%= escape_javascript(render partial: "shared/mypartial")%>
But I get the following error
throw Error("NoMethodError: undefined method `render' for #<# <Class:0x007fb68f801a40>:0x007fb68d3c3fe8>\n (in /Users/user/Sites/app/app/assets/javascripts/application/app.js.coffee.erb)")
Any idea why is not getting the render method?
Coffeescript files aren't processed within the context of a request, therefore you won't be able to access the render method.

What statement can I use in Rails 3 to render HTML from a small static file?

I have a file in:
RAILS_ROOT/public/system/pages
It is a snippet of HTML. I would like to render it, along with other things, in one of my views.
But it seems like when I try to do a render from a view, Rails is always looking for a partial. But it doesn't pick up the file even when I name it with a leading underscore. How can I read and display this HTML snippet within a view?
have you tried with
<%= render :file => 'your/path/', :layout => false %>
inside the erb?

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>"

Rails 3: Simple AJAXy Page updates?

I can't believe I've been looking four hours for this simple task, but I have.
In Rails 2.3, I could replace one section of a page with this simple code:
render :update do |page|
page.replace_html "div_id", :partial => "new_content",...
end
In Rails 3, Ryan Bates has me writing entire new javascript functions, switching from Prototype (rails default) to jQuery, and otherwise not enjoying life. The other tutes are no more straightforward.
What am I missing? How do we replace a <div> these days?
Thanks, guys. The official answer seems to be that, yes, the team felt simple is the enemy of good and made it more complicated.
The first key is to create a .js.erb file NAMED for the method CALLING the ajax update. So if the index method handles the update, put the raw javascript in index.js.erb. This goes in the views folder.
Second, the code that worked in index.js.erb was
m = $('list_users');
m.innerHTML = "<%= escape_javascript(render :partial => "reload_users") %>";
Then to make the call, add in the respond_to block of the controller method, add:
format.js
Finally, the calling view has:
<%= link_to "Update User List", #reload_users_path, :remote => true %>
By the way, supposedly all the old pages using page.replace will work if you install a plugin. The plugin download page suggests that it broke in the last releases of Rails 3 and has not been fixed. Also, various bloggers will come to your home and birch-switch you if you use it.
The whole RJS stuff makes the javascript inline and makes the dom very obtrusive. Also, by avoiding inline javascript you could open up other possible ways of optimizing you javascript by compressing and caching those files in browser. Thats the reason why RJS is getting out of scope from rails 3. A little bit of getting around with jQuery or Prototype for a day should get you on gears with these kind of small stuff and will help the project on long run.
Do you still have jQuery in there? I'd recommend it over Prototype any day...
If it's still there you can just use the following in your Javascript:
$.get("<%= url_for path/to/partial %>",
function(response) {
$("#div_id").html(response);
});
This gets the partial via AJAX and just dumps it into the div with id div_id.
Hope this helps!
I'm not even sure you need to make an AJAX call to load that partial. I believe that in a js.erb file, a call to render(:partial => object_or_path) will just return a string with all the html, which you can wrap in a jQuery object and append. Example:
$('#div_id').html($('<%= render :partial => #object %>'))
As far as I know, along the same line as the answer above, you can do something like this in your template:
<%= link_to "Update User List", #reload_users_path, :remote => true %>
And in controller, do this:
respond_to do |format|
format.js {
render :text => "alert('reloaded')"
}
end
This way you can have controller "execute" client side JS much the same as as render :update used to do. This is equivalent to doing the following in Rails 2:
render :update do |page|
page << "alert('reloaded')"
end
Is there any reason why this approach is not advisable?
Try this:
page.call "$('#div_id').html", render(:partial => 'new_content')

Resources