我有以下代码:
评论.js.erb
alert("Alert");
应用程序.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
and respond to
,但它仍然没有运行。