Rails form_for :remote=>true is not calling js method

前端 未结 7 1699
感情败类
感情败类 2020-12-01 09:11

I have no idea why this is not working, I\'m learning rails and I\'m following a book it says to do it like this:

    <%= form_for([@article,@article.comm         


        
相关标签:
7条回答
  • 2020-12-01 09:35

    This error occurs when you have not add jquery_ujs file. You just included the jquery file.

    So you need to add both files manually in you view or require them in application.js or any other file which you are using for specific layout.

    Depending upon your scenario, you can follow 1st or 2nd solution.

    1st solution:

    <%= javascript_include_tag :jquery, :jquery_ujs %>
    

    2nd solution:

    1. Require both jquery and jquery_ujs in app/assets/application.js

      //= require jquery.js
      
      //= require jquery_ujs
      
    2. Include application.js file in your specific layout file.

      <%= javascript_include_tag :application %>
      

    Note:

    Also add csrf tag in layout file for both solutions.

    <%= csrf_meta_tag %>
    

    I'm asuming that you have below code in your Gemfile and already installed this Gem.

    gem 'jquery-rails'
    
    0 讨论(0)
  • 2020-12-01 09:39

    Check your /app/assets/javascript/application.js file

    It should be included below files in there. jquery_ujs is responsible for working remote=> true feature.

    require `jquery`
    require `jquery_ujs`
    

    Happy Coding!!

    0 讨论(0)
  • 2020-12-01 09:41

    Check the controller: does it have a respond_to block?

    If you generated your app with rails generate scaffold, it will have something like

        respond_to do |format|
          format.html
          format.xml  { .... }
        end
    

    If it does have it, just take the whole block out, from respond_to to end; or alternatively replace the whole block with the single line

        respond_to :html, :js
    

    or alternatively edit the block by adding the extra line shown here

        respond_to do |format|
          format.html
          format.js
          format.xml  { .... }
        end
    

    Either should work in your case.

    0 讨论(0)
  • 2020-12-01 09:54

    When you check the request made in firebug, just because the url did not end with .js, it does not mean that it was not called from javascript. To verify that you should check the request header to see what the Accept parameter is. If it says "application/javascript" then it's all good.

    Secondly, a very common problem when starting to try out the :remote => true is that the required javascript libraries are not included in your code. So my guess is that the following code is missing from your layout:

    <%= javascript_include_tag :defaults %>
    <%= csrf_meta_tag %>
    

    If that is the case, just include it inside the <head> tag of your layout.

    0 讨论(0)
  • 2020-12-01 09:57

    If you have a file_field then it submits as html, even when remote: true - took me a long time to work that out so I am adding this answer to maybe save someone else time, there is a solution, use the remotipart gem, see the answer here

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

    Most likely what happens is that you are missing the rails.js file, which handles that for you, whether you are using prototype or jquery.

    If you are using jQuery, the easiest way to get all the needed files is using the jquery-rails gem. This will add a generator to install jquery and the needed rails.js.

    type something like inside your rails application root:

    rails g jquery:install
    

    And then, inside your application.html.erb add the line

    <%= javascript_include_tag :defaults %>
    

    or explicitly (do not forget to include your jquery separately):

    <%= javascript_include_tag :rails, :application %>
    

    [EDIT: for Rails 3.1 or greater using the asset pipeline]

    Use the jquery-rails gem (as mentioned above) and add the following lines to the app/assets/javascripts/application.js (if they are not there already) :

    //= require jquery
    //= require jquery_ujs
    

    Hope this helps!

    0 讨论(0)
提交回复
热议问题