简体   繁体   English

Rails:在AJAX提交后形成部分不清除/重新加载

[英]Rails: Form partial not clearing/reloading after AJAX submission

I am trying to create a one page/view application. 我正在尝试创建一个页面/视图应用程序。 I have a form I submit that creates a simple tweet object: 我有一个提交的表单,它创建一个简单的推文对象:

_form.html.erb _form.html.erb

<div id="schedule-tweet-form">
  <%= simple_form_for @tweet, :remote => true do |f| %>
    <%= f.input :text, :input_html => {:cols => 60, :rows => 6} %>
    <%= f.input :scheduled_time %>
    <%= f.button :submit, "Schedule Tweet", :class => "f-button" %>
  <% end %>
</div>

(scheduled_time is a datetime type, has multiple select boxes) (scheduled_time是日期时间类型,有多个选择框)

The form submission works fine and creates a row in my database without a problem. 表单提交工作正常,并在我的数据库中创建一行没有问题。 The issue is that after it submits, the data in the form is not cleared. 问题是,在提交后,表单中的数据不会被清除。 The text for the tweet is still in the input textbox and the scheduled time still has its respective select boxes chosen. 推文的文本仍在输入文本框中,预定时间仍然选择了相应的选择框。

Here is my controller: 这是我的控制器:

tweets_controller.rb tweets_controller.rb

class TweetsController < ApplicationController
  def index
    @tweet = Tweet.new
  end

  def create
    @tweet = Tweet.new(params[:tweet])
    @tweet.user = current_user

    respond_to do |format|
      if @tweet.save
        @tweet.tweet_at_scheduled_time
        flash[:notice] = "Tweet has been scheduled"
        format.html { render 'index' }
        format.js
      else
        format.html { render 'index' }
        format.js { render 'reload' }
      end
    end
  end
end

Here is the view where the partial is rendered: 以下是渲染部分的视图:

index.html.erb index.html.erb

<div id="schedule-tweet-form-container">
  <%= render 'form' %>
</div>

I tried to use some jQuery to remove the form and re-render a new one once the form is submitted and the create action is called, but weirdly the form comes back with the submitted tweet's information after "append" is called (text & scheduled_time data in the input fields). 我试图使用一些jQuery来删除表单,并在提交表单并调用create动作后重新呈现一个新表单,但奇怪的是,在调用“append”之后,表单会返回提交的推文信息(text&scheduled_time输入字段中的数据)。

create.js.erb create.js.erb

<% if @tweet.save %>
  $("#schedule-tweet-form").remove();
  $('#schedule-tweet-form-container').append("<%= j render 'form' %>");
<% end %>

I have been racking my brain on this the past couple of hours and nothing else I tried from various other stack overflow questions has worked. 在过去的几个小时里,我一直在绞尽脑汁,没有其他任何我尝试过的各种其他堆栈溢出问题都有效。 I appreciate the help. 我很感激帮助。

In your create action, after saving the tweet, you need to initialize the Tweet object again. 在您的创建操作中,保存推文后,您需要再次初始化Tweet对象。

But in your create.js.erb you have used the @tweet, so modify the code as following 但是在你的create.js.erb中你已经使用了@tweet,所以修改代码如下

<% if @tweet.save 
     @tweet = Tweet.new
%>

  $("#schedule-tweet-form").remove();
  $('#schedule-tweet-form-container').append("<%= j render 'form' %>");
<% end %>

you should use 你应该使用

    format.json 

on ajax calls 在ajax电话上

check the following answer of this thread to make simple form make a json post 检查这个线程的以下答案,使简单的表格成为一个json帖子

Is there a way to submit ajax/json requests with simple_form for Rails 有没有办法使用simple_form for Rails提交ajax / json请求

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM