Error while rendering partial using escape_javascript in Rails?
Your @search_financial_institutions
variable is an Active Record relation, a collection of records. You're trying to call financial_institution
on that collection, and the collection doesn't respond to that method, the individual records in that collection do.
If you want to display every record in your collection, try changing your search/_financial_institution.html.erb file to:
<% institutions.each do |institution| %>
<option><%=institution.financial_institution%></option>
<% end %>
Rails escape_javascript breaks javascript used in partial
This is what happens:
After your site was loaded, your js kicks in and binds the click event handler to #relation-contact-button. Then you do whatever and call your ajax handler
$("#datas-user").html("<%= escape_javascript(render :partial => '/pro/users/show_partial', :locals => { :id => @user.id }) %>");
everything inside #datas-user is removed and replaced with your partial and no event listener is bound to anything, since the element which your handler was bound to is gone. You have to either bind your handler to the newly rendered #relation-contact-button again in your ajax success handler, or use:$("#datas-user").on("click", "#relation-contact-button", function(){
$('#relation-contact').show();
});
instead of$("#relation-contact-button").click(function(){
$('#relation-contact').show();
See the .on() doc for more information. Rails 4: escape_javascript render partial not working
The code under app/assets
can not call render
. Actually it can not call almost anything except a very few methods.
Move it somewhere to app/views/somethings/analysis.js.erb
Where somethings
is the name of your controller in plural form, so just put it near the _analysis.html.erb
.
Rendering a partial and escaping the html/javascript for a bookmarklet
I don't know why you try to eval
something if I understand what you want to do correctly.
Use json
and render_to_string
if you want to pass html by javascript. So for your example:
# controller
def action_name
content =
render_to_string(
partial: 'events/bookmarklet',
formats: [:html], # always render html format, even if request wants json
layout: false # render only partial, without any layout
)
render json: { html: content }
end
You can access the content in javascript by data.html
then. Escape Javascript when rendering a html.erb partial from within a controller
I thought more complicated than it really is. All stuff using <%= %> is by secure by default (as HTML tags are automatically escaped in Rails >= 3). And so the above scenario is also no risk as the database content is inserted that way into the partial.
Partial won't render when replacing html with jQuery and escape_javascript
I have put my js in a js.erb-file, and finally managed to use my variables in the right way.
Here's my code:
$('#id' + '<%= @id %>' + '_show').html('<%= escape_javascript(raw render :partial => 'form', :locals => {:@resource => Resource.find_by_id(@id)}).html_safe %>')
And in the controller:format.js { @id = params[:id]}
In the view: <%= link_to t('edit'), resources_path(:id => resource.id), :remote => true %>
Related Topics
Event.Wheeldelta Returns Undefined
Convert CSV Data into JSON Format Using JavaScript
JavaScript Getelementbyname Doesn't Work
How to Distinguish Mouse "Click" and "Drag"
Why Can't I Assign a New Value to "This" in a Prototype Function
Cross Domain Localstorage with JavaScript
JavaScript Time Zone Is Wrong for Past Daylight Saving Time Transition Rules
How to Parse a Date in Format "Yyyymmdd" in JavaScript
Why "This" Is Undefined Inside a Fat Arrow Function Definition
How to Reload/Refresh Jquery Datatable
Quickest Way to Pass Data to a Popup Window I Created Using Window.Open()
Get Visitors Language & Country Code with JavaScript (Client-Side)
Create Copy of Multi-Dimensional Array, Not Reference - JavaScript
Passing Data to a Jquery UI Dialog
In JavaScript, Can You Extend the Dom