jQuery + Ajax + Haml. js.erb files not firing

断了今生、忘了曾经 提交于 2019-11-28 12:50:32

I found the answer! The issue was that when the controller was rendering the view, it was including the overall layout of my application; the layout was yielding to the rendering action, so my javascript code contained inside of my .js.erb file was spit out into my application.rhtml. I fixed this issue by including this inside of my controller action to display my posts:

respond_to do |format|
  format.js {render :layout=>false}
end

Watch a Railscast

<%= form_tag products_path, :method => 'get', :id => ↵  
  "products_search" do %>  
  <%= hidden_field_tag :direction, params[:direction] %>  
  <%= hidden_field_tag :sort, params[:sort] %>  
  <p>  
    <%= text_field_tag :search, params[:search] %>  
    <%= submit_tag "Search", :name => nil %>  
  </p>  
<% end %>

and in Js

// Search form.  
  $('#products_search').submit(function () {  
    $.get(this.action, $(this).serialize(), null, 'script');  
    return false;  
  });  
});

When my layout was coded in Haml (application.haml), AJAX wouldn't fire and the work-around code kelly.dunn mentioned didn't work.

respond_to do |format|
  format.js {render :layout=>false}
end

The easiest fix was to convert the application layout to .html.erb format.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!