我使用一些代码在Rails 3上很好地工作,但不在Rails 4上,我想这是由Turbolinks引起的,但我对它不了解,不能深入挖掘解决我的问题,这里是代码:Rails 4 Turbolinks使表单提交多次
观点:
a/v/m/_new_comment.slim
.new-comment
- if current_user
= render "editor_toolbar"
= form_for(Comment.new, :remote => true, :url => mission_comments_path(@mission)) do |f|
= f.text_area :content, :class => "span10",
:rows => "4", :tabindex => "1"
#preview.hidden
= "Loading..."
= f.submit t("missions.submit_comment"),
"data-disable-with" => t("missions.submitting"),
:class => "btn btn-primary", :tabindex => "2"
- else
= render "need_login_to_comment"
控制器:
def create
@mission = Mission.find(params[:mission_id])
@comment = @mission.comments.build(comment_params)
@comment.user = current_user
if @comment.save
@mission.events.create(user: current_user, action: "comment")
render layout: false
end
和JS:
<% if @comment.errors.any? %>
$(".new-comment textarea").focus();
<% else %>
$(".comments").append("<%= j (render @comment, :index => @mission.comments.count-1) %>");
$(".new-comment #preview").addClass("hidden").html('');
$(".new-comment textarea").css("display", "block").val('');
$(".editor-toolbar .preview").removeClass("active");
$(".editor-toolbar .edit").addClass("active");
<% end %>
我有两个问题,关于这个问题,首先:像这样的控制器代码是不行的 的js代码是转移到客户端,但不跑,我必须在该诉讼的底部添加render layout: false
,没必要这样on rails 3
第二个问题:当我第一次访问这个页面时,重新加载页面,评论函数的作品,但是如果我点击了其他页面的链接跳转到这个页面,我提交这个表单会导致ajax请求调用multiple次,将会创建多个评论
谢谢你的支持
我不能在你的特定代码发表评论,但你可以尝试删除涡轮链接并查看它是否可用。 [删除turbolinks的说明](http://blog.steveklabnik.com/posts/2013-06-25-removing-turbolinks-from-rails-4) –
只是从application.js文件中删除turbolinks使其工作 –