我正在用Rails编写一个窗体,它将通过ajax编辑现有的问题。Rails update.js.erb没有执行javascript
提交表单并且问题已更新后,控制器中的更新方法会呈现update.js.erb,这会再次隐藏表单。
我的问题是,update.js.erb中的javascript代码根本没有执行。
我知道该文件被渲染,因为它在服务器输出显示出来,当我把
<% raise params %>
到它,它的工作原理。
然而,即使是最简单
alert('hello');
在同一个文件中没有任何影响。
我排除了JavaScript和jQuery配置问题,因为相同的代码在我的edit.js.erb文件中完美地工作。它只是在update.js.erb中不起作用。
我错过了什么?
编辑:
萤火未显示任何错误。这里是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>');
高清更新
编辑2:
这是控制器的动作:
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
结帐在Firebug的网络面板的响应。它显示了什么? – Arjan
添加了对问题的回复。谢谢参观。 – dmanaster
显示您是否在表单中创建了ajax?例如,如果你只是调用$ .ajax(..),但是对响应什么也不做将会被执行,与例如使用jQuerys getScript(http://api.jquery.com/jQuery.getScript/)相反 – trueunlessfalse