我有以下代碼:爲什麼我js.erb不工作
comment.js.erb
alert("Alert");
的application.js
jQuery.ajaxSetup({
'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")}
})
jQuery.fn.submitWithAjax = function() {
this.submit(function() {
$.post(this.action, $(this).serialize(), null, "script");
return false;
})
return this;
};
$(document).ready(function() {
$(".comment_form").submitWithAjax();
})
查看形式:
<% form_for :comment, :url => comment_task_path(tasks.id),
:html => {:remote => true,
:class => "comment_form"} do |f|-%>
<%= f.text_field :remark, :placeholder => "Add Comments", :rows => 2,
:class => 'box',
:style => "width: 834px; height: 40px;"%>
<%= f.submit "Comment"%>
<% end -%>
控制器方法:
def comment
@comment = Comment.new(params[:comment])
@comment.user_id = @current_user.id
@task.comments << @comment
flash[:notice] = "thank you"
if @comment.save
# what code do I put here to render comment.js.erb?
else
end
end
如果我想要comment
方法渲染我的comment.js.erb,需要輸入哪些代碼? 我試過render to
和respond to
,但它仍然沒有運行。
請花時間正確縮進您的代碼。你也不會顯示遠程相關的代碼片段。你的表單是':update_status',它與'.comment_form'無關。 – meagar 2013-03-20 04:05:28
對不起,我更新了它。我的錯誤 – 2013-03-20 04:09:31
在'comment.js.erb'文件夾中? '應用程序/視圖/ tasks'?你在development.log中得到什麼錯誤信息? – Mischa 2013-03-20 05:57:56