Ruby-on-rails Rails form_for :remote=>true 没有调用 js 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4227775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:40:16  来源:igfitidea点击:

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

ruby-on-railsruby-on-rails-3

提问by ryudice

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:

我不知道为什么这不起作用,我正在学习 rails 并且我正在关注一本书,它说这样做是这样的:

    <%= form_for([@article,@article.comments.new ], :remote=>true, :html => {:style=>'display: none;' }) do |f|%>
    <div class="field">
      <%=    f.label :name %>
      <%=    f.text_field :name %>
    </div>
    <div class="field">
      <%=    f.label :email %>
      <%=    f.text_field :email %>
    </div>
    <div class="field">
      <%=    f.label :body %>
      <%=    f.text_area :body %>
    </div>
    <%= f.submit %>
<% end %>

However it does not work, and when I check the request in firebug the url doesnt end in .js, do you have any ideas why?

但是它不起作用,当我在 firebug 中检查请求时,url 不以 .js 结尾,你知道为什么吗?

回答by DanneManne

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.

当您检查在 firebug 中发出的请求时,仅仅因为 url 没有以 .js 结尾,并不意味着它不是从 javascript 调用的。要验证您是否应该检查请求标头以查看 Accept 参数是什么。如果它说“应用程序/javascript”,那么一切都很好。

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:

其次,开始尝试 :remote => true 时,一个非常常见的问题是所需的 javascript 库未包含在您的代码中。所以我的猜测是您的布局中缺少以下代码:

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

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

如果是这种情况,只需将其包含在<head>布局的标签中。

回答by nathanvda

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

最有可能发生的情况是您丢失了rails.js为您处理该文件的文件,无论您使用的是原型还是 jquery。

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

如果您使用的是 jQuery,那么获取所有所需文件的最简单方法是使用jquery-railsgem。这将添加一个生成器来安装 jquery 和所需的 rails.js。

type something like inside your rails application root:

在 Rails 应用程序根目录中输入类似的内容:

rails g jquery:install

And then, inside your application.html.erbadd the line

然后,在你的application.html.erb添加行中

<%= javascript_include_tag :defaults %>

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

或明确地(不要忘记单独包含您的 jquery):

<%= javascript_include_tag :rails, :application %>

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

[编辑:对于使用资产管道的 Rails 3.1 或更高版本]

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

使用jquery-railsgem(如上所述)并将以下几行添加到app/assets/javascripts/application.js(如果它们还没有):

//= require jquery
//= require jquery_ujs

Hope this helps!

希望这可以帮助!

回答by Manish Shrivastava

Check your /app/assets/javascript/application.jsfile

检查您的/app/assets/javascript/application.js文件

It should be included below files in there. jquery_ujsis responsible for working remote=> truefeature.

它应该包含在那里的文件下面。jquery_ujs负责工作remote=> true功能。

require `jquery`
require `jquery_ujs`

Happy Coding!!

快乐编码!!

回答by Taimoor Changaiz

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

当您尚未添加jquery_ujs文件时会发生此错误。您刚刚包含了 jquery 文件。

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

因此,您需要在视图中手动添加这两个文件,或者在application.js或用于特定布局的任何其他文件中要求它们。

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

根据您的情况,您可以遵循第一种或第二种解决方案。

1st solution:

第一个解决方案:

<%= javascript_include_tag :jquery, :jquery_ujs %>

2nd solution:

第二种解决方案:

  1. Require both jqueryand jquery_ujsin app/assets/application.js

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

    <%= javascript_include_tag :application %>
    
  1. app/assets/application.js 中同时需要jqueryjquery_ujs

    //= require jquery.js
    
    //= require jquery_ujs
    
  2. 在您的特定布局文件中包含 application.js文件。

    <%= javascript_include_tag :application %>
    

Note:

笔记:

Also add csrf tag in layout file for both solutions.

还为这两种解决方案在布局文件中添加 csrf 标签。

<%= csrf_meta_tag %>

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

我假设您的 Gemfile 中有以下代码并且已经安装了这个 Gem。

gem 'jquery-rails'

回答by Andrea

Check the controller: does it have a respond_to block?

检查控制器:它是否有 respond_to 块?

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

如果您使用 rails generate scaffold 生成您的应用程序,它将具有类似

    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

如果确实有,只需取出整个块,从 response_to 到 end;或者用单行替换整个块

    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.

两者都应该适用于您的情况。

回答by ryan2johnson9

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

如果你有一个 file_field 那么它会作为 html 提交,即使remote: true- 我花了很长时间来解决这个问题,所以我添加这个答案可能会节省其他人的时间,有一个解决方案,使用 remotipart gem,在这里看到答案

回答by vidriloco

Change:

改变:

[@article,@article.comments.new ] 

for

为了

@article

I suggest you reading the API on the related section : http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html

我建议您阅读相关部分的 API:http: //api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html