I need to put some javascript inside a view. Basically I am having a play with the Recurly.js library.
Here is an example form: http://pastie.org/3142536
I have tried adding the JS to a partial: filename _recurly.js.erb
<%= render :partial => 'recurly.js', :locals => { :company => #company } %>
But its just outputting the JS to the page.
Is there a better way of doing this passing vars to JS for output and how can I get it to render the JS ?
Hope you can advise.
You need to use the function escape_javascript.
so your code should be:
<%= escape_javascript(render(:partial => 'recurly.js', :locals => { :company => #company }) %>
have you tried:
<%= raw(render :partial => 'recurly.js', ...) %>
Normally ERB will escape the output. Not sure if this is a good solution in the long run, even if it works, though.
why don't you use normal way of including javascript in your ERB view.
<%= javascript_include_tag "filename_from_public_javascripts_folder" %>
Related
I'm trying to build out an app and I started getting confused when to use a partial as opposed to simply refactoring code. In other words, when should I use 'render' and 'render partial: ........"
And if I put something in the "shared" folder under "views" does that make it a partial? Not sure when to use these different folders. Thanks a whole bunch!
Normally you use 'render' for just move some html codes, like "footer".
If you want the partial with its own layout or pass variables in it. We will use 'render :partial'
<%= render "footer" %> # Basic usage
<%= render "shared/footer" %> # _footer.html will be placed in "shared/_footer.html"
<%= render :partial => "sidebar", :layout => "sidebar_layout" %>
# It will using "_sidebar_layout" as a layout template for "_sidebar.html"
<%= render :partial => "form", :locals => { :post => #post } %>
# Passing #post variable as post in form partial
Reference: http://guides.rubyonrails.org/layouts_and_rendering.html#using-partials
I am new to Ruby on Rails and i am working through a few example applications in the O'Reilly Head First Rails book. In one of the examples there is a page made up of three partials. The middle partial is a list of items. There is a link right below this section that, when clicked, should refresh the div containing that partial. The book is running examples based off of Rails 2.3 i believe and i am using Rails 3.1. This is the example that the book is giving me:
routes.rb:
map.connect '/flights/:flight_id/seats', :action=>'flight_seats', :controller=>'seats'
seats_controller.rb:
def flight_seats
#flight = Flight.find(params[:flight_id])
render :partial => "flights/seat_list", :locals => {:seats => #flight.seats}
end
show.html.erb:
<div id="seats">
<%= render :partial=>"seat_list". :locals=>{:seats=>#flight.seats} %>
</div>
<$= link_to_remote("Refresh Seats", :url=>"/flights/#{#flight.id}/seats", method=>"get", :update=>"seats") %>
This example is also using prototype.js since that's what Rails 2.3 came with built in. Rails 3 has jQuery as the default JavaScript library. (not sure if that makes a big difference)
Here is what i have so far. This is getting the contents of the partial correctly, it's just not updating the "seats" div after the AJAX call gets the partial. My code:
routes.rb:
match 'flights/:flight_id/seats' => 'seats#flights_seats'
seats_controller.rb:
def flights_seats
#flight = Flight.find(params[:flight_id])
render :partial => "flights/seat_list", :locals => { :seats => #flight.seats }
end
show.html.erb:
<div id="seats">
<%= render :partial => 'seat_list', :locals => { :seats => #flight.seats } %>
</div>
<%= link_to "Refresh Seats", "/flights/#{#flight.id}/seats", :remote => true %>
Any idea why my <div id="seats"> won't refresh with the updated partial? I'm betting there is but i'll ask anyway, is something wrong with my code?
The :remote => true option is a bit weird if you aren't returning JSON data. You can wrap your HTML in a JSON object, though, which is what I typically do. Or if you want something closer to your existing code something like this should work for you:
<%= link_to "Refresh Seats", "/flights/#{#flight.id}/seats", :class => "refresh-seats" %>
In your javascript somewhere:
$(document).delegate(".refresh-seats", "click", function(e){
e.preventDefault();
$("#seats").load(this.href);
});
I am using rails 3.1. I have a view products/show.html.erb and I call a partial like this
<%= render 'productrelationships/relatedproduct',:collection => #product.relatedproducts %>
and i access it in this way inside my partial (productrelationship/_relatedproduct)
<% logger.error 'Related Products ' + relatedproduct.inspect %>
The inspect returns a nil. But if I try the same inside my show.html.erb, it is not nil. There is some mistake in passing the value. What am I doing wrong?
Found the answer. It started working when i added :partial while rendering
<%= render :partial => 'productrelationships/relatedproduct',:collection => #product.relatedproducts %>
Need to specify the local variable.
<%= render :partial => 'productrelationships/relatedproduct',
:collection => #product.relatedproducts,
:as => :relatedproduct %>
I would like to render the contents of an action (e.g new_sub_batch) inside a div.
I tried
<div id="newBatch">
<%= render :template => 'new_sub_batch.html.erb' %>
</div>
but nothing is displayed.
I even tried <%= render :action => 'new_sub_batch' %>..still nothing.
Any suggestion??
Thanks a lot
What you want are partials. Distil the common markup that both views will use into a single file, and prefix its name with an underscore. Then call render :partial => 'filename', where filename is the name of the partial without the underscore.
In your case, the code you pull out of new_sub_batch.html.erb might go in a _batch.html.erb partial, in the same directory as your other sub_batch views. You would render this partial with:
render :partial => 'batch'
In Rails3, you can simply use render 'batch'.
If you want to pass a variable to the partial, you can do so via :locals. Assuming you have a #sub_batch variable you want to pass, your call would look something like this:
render :partial => 'batch', :locals => { :sub_batch => #sub_batch }
While this doesn't strictly answer your question, I believe within the ruby-on-rails tag it's more important to explain the Rails Way, rather than help you do it the wrong way.
Try file render:
<div id="newBatch">
<%= render :file => 'directory/new_sub_batch.html.erb' %>
</div>
So I've got a form in my Rails app which uses a custom FormBuilder to give me some custom field tags
<% form_for :staff_member, #staff_member, :builder => MyFormBuilder do |f| %>
[...]
<%= render :partial => "staff_members/forms/personal_details", :locals => {:f => f, :skill_groups => #skill_groups, :staff_member => #staff_member} %>
[...]
<% end %>
Now, this partial is in an area of the form which gets replaces by an AJAX callback. What I end up doing from the controller in response to the AJAX request is:
render :partial => "staff_members/forms/personal_details", :locals => {:skill_groups => #skill_groups, :staff_member => #staff_member}
However, if I do that then the form breaks, as the FormBuilder object I used in the form_for is no longer available. Is there any way for me to use my custom FormBuilder object inside a partial used for an AJAX callback?
Use fields_for inside your partial. It performs a similar task but without wrapping the form tags. See the API docs.
how about this?
#template.with_output_buffer do
#template.form_for #model_object do |f|
f.fields_for :some_nested_attributes do |ff|
render :partial => 'nested_attributes', :object => #model_object, :locals => {:form => ff}
end
end
end
this would be especially useful is you need to use the nested fields_for in the partial
You could instantiate a new instance of your form builder in the controller, though it feels sort of lousy to me:
# in the controller
render :partial => {
:f => MyFormBuilder.new(:staff_member, #staff_member, template),
:skill_groups => #skill_groups,
:staff_member => #staff_member
}
Alternatively, you could move more of the update logic to be client side which wouldn't require you to worry about rendering anything at all. You could just update the values via JS. Not sure if that works for your project though.
Maybe I'm a little late in the game here, and maybe I don't understand the question properly, but in ApplicationHelper.rb I think you can just add the line:
ActionView::Base.default_form_builder = MyFormBuilder
You can submit within your ajax call the content of f.object_name (it's also works with partials) and use it to render tags defined in http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html passing it as the first argument.