Hi I am calling a partial from controller file but the javascript inside the partial is not executing . The partial is of .html.erb format.
<% content_for :javascript do %>
<script type="text/javascript">
var eventName = typeof(Turbolinks) !== 'undefined' ? 'page:change' : 'DOMContentLoaded';
document.addEventListener(eventName, function() {
console.log("hello");
<% if flash[:notice] %>
ShopifyApp.flashNotice("<%= escape_javascript flash[:notice].html_safe %>");
<% end %>
<% if flash[:error] %>
ShopifyApp.flashError("<%= escape_javascript flash[:error].html_safe %>");
<% end %>
});
</script>
<% end %>
is the code for partial
respond_to do |format|
if @timer.save
flash[:notice]="updated"
format.html{render :partial => "layouts/flash_messages", :locals => {:flash => flash}}
else
flash[:error]="error"
format.html{render :partial => "layouts/flash_messages", :locals => {:flash => flash}}
end
is the controller code .
If you want to render Javascript in rails use below method.
Controller Code looks like:
def create
@user = User.find(params[:id])
respond_to do |format|
format.html {redirect_to @user }
format.js
end
end
template this code going to render will look like.
filename: create.js.erb
$("#someidname").html("<%= @user.email %>");
Note @user is also available here. you can render other partial also like:
$("#someit").html("<%= escape_javascript(render('users/email')) %>");
Also, make sure you are sending ajax request.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.