jQuery Rails update.js.erb 不执行 javascript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18936633/
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-08-26 22:48:39  来源:igfitidea点击:

Rails update.js.erb not executing javascript

javascriptjqueryruby-on-railsajaxruby-on-rails-3

提问by dmanaster

I am building a form in rails that will edit an existing question via ajax.

我正在 Rails 中构建一个表单,该表单将通过 ajax 编辑现有问题。

After the form is submitted and the question has been updated, the update method in the controller renders update.js.erb, which will hide the form again.

提交表单并更新问题后,控制器中的更新方法呈现update.js.erb,它将再次隐藏表单。

My problem is that the javascript code in update.js.erb is not executing at all.

我的问题是 update.js.erb 中的 javascript 代码根本没有执行。

I know that the file is rendering because it shows up in the server output, and when I put a

我知道该文件正在呈现,因为它显示在服务器输出中,并且当我放置

<% raise params %>

into it, it works.

进入它,它的工作原理。

However, even the simplest

然而,即使是最简单的

alert('hello');

has no effect in the same file.

在同一个文件中没有影响。

I've ruled out javascript and jquery configuration issues because the same code works perfectly in my edit.js.erb file. It's just not working in update.js.erb.

我已经排除了 javascript 和 jquery 配置问题,因为相同的代码在我的 edit.js.erb 文件中完美运行。它只是在 update.js.erb 中不起作用。

What am I missing?

我错过了什么?

Edit:

编辑:

Firebug shows no errors. Here is the response in firebug's network panel:

Firebug 没有显示错误。这是 firebug 的网络面板中的响应:

alert('hello');
$('#question_body').replaceWith('<h4><p>jhsdfjhdsb k jdfs j fjfhds <strong>jfshaflksd;hf sdldfs l fdsalkhdfskhdfs</strong>;fd lfdksh hfdjaadfhsjladfhsjadfs ;df sjldfsj dfas hafdsj fdas ;ldfas ldfs df dl;hdf fdh ;fdj ;lfads</p></h4>');

def update

定义更新

Edit 2:

编辑2:

This is the controller action:

这是控制器操作:

def update
  respond_to do |format|
    if @question.update_attributes(params[:question])
      format.html { redirect_to @question, :flash => { :success => 'Question was successfully updated.' } }
      format.json { head :no_content }
      format.js {}
    else
      format.html { render action: "edit" }
      format.json { render json: @question.errors, status: :unprocessable_entity }
    end
  end
end

回答by supermoogle

In your $.ajaxcall make sure to set the dataTypeoption to "script"otherwise the response could be interpreted in other ways and thus not executed as JS.

在您的$.ajax通话中,请确保将dataType选项设置为"script"否则响应可能会以其他方式被解释,因此不会作为 JS 执行。

回答by rmatena

Do you work with haml, or html.erb? If the former, then this might be the solution:

你使用 haml 还是 html.erb?如果是前者,那么这可能是解决方案:

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

I had the exact same problem, found this question early on, took another hour or so of Googling to find this question on StackOverflow that led me to it: jQuery + Ajax + Haml. js.erb files not firing

我遇到了完全相同的问题,很早就发现了这个问题,又花了一个小时左右的谷歌搜索在 StackOverflow 上找到了这个问题,这让我找到了这个问题:jQuery + Ajax + Haml。js.erb 文件未触发

回答by Ricky Mason

In your update.js.erb file you need to escape javascript wherever you execute ruby code.

在您的 update.js.erb 文件中,您需要在执行 ruby​​ 代码的任何地方转义 javascript。

$('.container').empty().append('<%=
  escape_javascript(
    render 'update'
  )
%>')

This is what solved it for me ...

这就是为我解决的问题......

回答by Hoang Le

This issue isn't just because of Controllerside. It is also can be in the Viewside which is you didn't clarify the data-type of your post request.

这个问题不仅仅是因为控制器方面。它也可以在视图端,即您没有阐明发布请求的数据类型。

Make sure in your console that the request is treated as JS.

确保在您的控制台中将请求视为JS

Reference: Similar issue

参考: 类似问题

回答by wyncg

I ran into the same issue and found this page. I tried methods listed here but found no luck. While later the following method solve my issue.

我遇到了同样的问题并找到了这个页面。我尝试了此处列出的方法,但没有找到运气。后来下面的方法解决了我的问题。

My originally code was:

我最初的代码是:

$('#html_id').html('<%=@ruby_variable%>');

And I updated it to:

我将其更新为:

$('#html_id').html('<%=raw @ruby_variable.to_json%>');

Now it works as expected.

现在它按预期工作。

回答by Daniela

Found out what it is! (solution for rails 4)

发现它是什么!(导轨 4 的解决方案)

If you have in your ajax call parameters that are not in your permitted list, the record gets saved, you get no error messages about the 'not permitted' parameters, but the update.js.erb won't run - even though your terminal will feed back 'Rendered update.js.erb etc'

如果您的 ajax 调用参数不在您的允许列表中,则记录将被保存,您不会收到有关“不允许”参数的错误消息,但 update.js.erb 不会运行 - 即使您的终端将反馈'Rendered update.js.erb etc'

If the extra parameter is an attribute in your model, just permit it.

如果额外的参数是您模型中的一个属性,请允许它。

The simplest way to permit non model parameter is to add in your model:

允许非模型参数的最简单方法是在模型中添加:

  attr_accessor :paramKeyTroublesome

Then you can also permit it in the controller.

然后你也可以在控制器中允许它。

in the $ajax call, data needs to be hashed up properly:

在 $ajax 调用中,数据需要正确散列:

  data: {model_name: {paramKey1: value, paramKeyTroublesome: value}}

回答by Shadoath

One more problem to be aware of is an error in your update.js file. Nothing will execute if there are any syntax errors. You can check this by going to your browser inspector and enabling Log XMLHttpRequestsThen reviewing the output js file.

另一个需要注意的问题是 update.js 文件中的错误。如果有任何语法错误,则不会执行任何操作。您可以通过转到浏览器检查器并启用Log XMLHttpRequests然后查看输出 js 文件来检查这一点。