Rails update.js.erb没有执行javascript [英] Rails update.js.erb not executing javascript

查看:127
本文介绍了Rails update.js.erb没有执行javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



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



我知道文件正在渲染,因为它显示在服务器输出中,当我放入

 <%raise params%> 

进入它,它有效。



但是,即使是最简单的

  alert('hello'); 

在同一档案中无效。



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



我缺少什么?



编辑:



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



编辑2:



这是控制器操作:

  def update 
respond_to do | format |
if @ question.update_attributes(params [:question])
format.html {redirect_to @question,:flash => {:success => '问题已成功更新。'}}
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


解决方案

$。ajax 中调用确保设置 dataType 选项到script否则响应可以用其他方式解释,因此不能作为JS执行。


I am building a form in rails that will edit an existing question via 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.

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

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.

What am I missing?

Edit:

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

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:

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

解决方案

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

这篇关于Rails update.js.erb没有执行javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆