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

前端 未结 3 1781
一整个雨季
一整个雨季 2020-12-11 09:15

After reading up on a few experiences, I feel this issue might need to be brought up again. Coding in Rails3, I\'m trying to implement some smooth Ajax effects when a user

相关标签:
3条回答
  • 2020-12-11 09:54

    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;  
      });  
    });
    
    0 讨论(0)
  • 2020-12-11 09:54

    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.

    0 讨论(0)
  • 2020-12-11 09:58

    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
    
    0 讨论(0)
提交回复
热议问题