【问题标题】:in Rails, render Json how to redirect conditionaly在 Rails 中,渲染 Json 如何有条件地重定向
【发布时间】:2021-01-08 22:51:54
【问题描述】:

在我的应用程序中,我有一个会议和一个联系人模型。 我已经使用 selectize 设置了一个 jquery 函数,它允许我直接在我的会议/新表单中创建一个新联系人。 为此,我在 ajax 中使用 POST 请求,并在 contacts_controller.rb 中为我的 create 方法渲染 json:

def create
  @contact = Contact.new(contact_params)
  @user = current_user
  @contact.user = @user

  if @contact.save!
    render json: @contact, except: :avatar
  else
  render json: { errors: @contact.errors.full_message }
  end
end

这种行为在我的会议/创建方法中是完美的,因为它允许我使用模态创建一个新联系人并在我的表单中获取我的函数的回调以显示新的联系人姓名。 这是我的会议/新观点:

*view : meeting/new*

<%= simple_form_for(@meeting) do |f| %>
  <%= f.input :name %>
  <%= f.input :description %>
  <%= f.input :start_date %>
  <%= f.input :end_date %>
  <%= f.association :contact, collection: Contact.all.pluck(:fullname, :id), input_html: { class: "select-contact" } %>

  <%= f.submit 'Enregistrer un nouvel rendez-vous', class:'btn btn-secondary' %>
<% end %>

<div class="modal fade contact-modal" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel">
  <div class="modal-dialog modal-sm" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
        <h4 class="modal-title" id="mySmallModalLabel">Ajouter un contact</h4>
      </div>
      <div class="modal-body">
        <%= simple_form_for Contact.new, remote: true do |f| %>
          <%= f.input :first_name %>
          <%= f.input :last_name %>
          <%= f.input :fullname %>

          <%= f.submit 'Ajouter un nouveau contact', class:'btn btn-primary' %>
        <% end %>
      </div>
    </div>
  </div>
</div>

我的问题是我还想通过我的联系人/新视图创建一个新联系人。但是在创建这个联系人之后,我会重定向到我的索引视图。

*view : contact/new*

<%= simple_form_for [ @user, @contact ] do |f| %>
  <%= f.input :avatar, as: :file %>
  <%= f.input :first_name %>
  <%= f.input :last_name %>
  <%= f.input :fullname %>
  <%= f.input :email %>
  <%= f.input :phone_number %>
  <%= f.input :tag_list %>
  <%= f.input :birthdate, as: :string, type: :text, input_html: {class: "datepicker", id: "birthdate"} %>
<%# raise %>
  <%= f.submit %>
<% end %>

由于我在联系人控制器中的 create 方法,当我的联系人被保存时它会触发渲染 json,但没有重定向

当我通过我的会议/新视图创建新联系人时,我将呈现 json 而不进行重定向。当我在联系人/新视图中创建新联系人时,我会在创建后重定向到我的联系人/索引视图。 在这两种情况下,我都需要发布一个 ajax 请求来存储一些数据。

有人知道我该怎么做吗?

非常感谢

【问题讨论】:

    标签: jquery ruby-on-rails json redirect rendering


    【解决方案1】:

    没错。 rails remote: true 表单功能不允许监听 HTTP 状态代码。 你必须使用 JavaScript 来做到这一点。

    您的问题类似于rails-redirecting-after-ajax-post

    如果您需要为此编写 JavaScript 的帮助,请告诉我。

    【讨论】:

    • 嗨,伙计,感谢您的回复。我尝试了你的建议。但实际上,我认为我的情况要复杂一些。在机器人案例中:通过联系人表单和联系人视图创建联系人,并通过会议表单和视图创建联系人,我需要发送 ajax 请求。但在第一种情况下(通过联系表格和联系人视图创建联系人)我会重定向到我的联系人(联系人控制器中的索引方法),而在第二种情况下(通过会议表单和视图创建联系人)我会重定向到我的会议(索引会议控制器中的方法)。
    • 这是我的 JS : $("#new_contact").on("submit", function(e) { e.preventDefault(); $.ajax({ method: "POST", url : $(this).attr("action"), data: $(this).serialize(), dataType: 'script', success: function(response) { window.location = '/meetings'; console.log( response); selectizeCallback({ value: response.id, text: response.fullname }); $(".contact-modal").modal('toggle'); // selectizeCallback = null; } }); });
    猜你喜欢
    • 1970-01-01
    • 2021-11-19
    • 2011-05-21
    • 1970-01-01
    • 2022-08-11
    • 2020-11-14
    • 2020-06-23
    • 2018-09-10
    • 2017-12-10
    相关资源
    最近更新 更多