且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Rails 5,带有表单提交的简单JavaScript

更新时间:2023-11-24 11:55:04

form_with这是一个远程表单,您不需要该块<script>..</script>. 只需使用create.js.erb,然后使用jquery来添加新注释.

form_with it's a remote form, you dont need that block <script>..</script>. Just use create.js.erb then use jquery to append new comment.

如果要检查是否成功创建了消息,则可以在控制器中添加@status值.

And if you want to check if your message was created successfully, you can add a @status value in your controller.

//comments_controller.rb
def create
  ...
  if @comment.save
    @status = "success"
  else
    @status = "failed"
  end
  ...
end

//create.js.erb - for example
 $("#yourCommentList").append('<%= j( render partial: 'comments/comment', locals: @comment ) %>');
 <% if @status == "success" %>
   $("body").append(...your notification...)
    ...
 <% else %>
    ...
 <% end %>