【问题标题】:AbstractController::DoubleRenderErrorAbstractController::DoubleRenderError
【发布时间】:2015-11-27 09:50:08
【问题描述】:

当我提交这个时:

<%= simple_form_for(@missed_date, url: habit_level_missed_dates_path({ habit_id: @habit, level_id: @habit.current_habit_level.id }), remote: request.xhr?, html: { data: { modal: true } }) do |f| %>

模态不会消失,因为页面没有 redirect_to :root_url 就像它在创建操作中所假设的那样。

  def create
    habit = Habit.find(params[:habit_id])
    habit.missed_days = habit.missed_days + 1
    habit.save!
    level = habit.levels.find(params[:level_id])
    level.missed_days = level.missed_days + 1
    if level.missed_days == 3
      level.missed_days = 0
      level.days_lost += habit.calculate_days_lost + 2
    end
    level.save!
    head :ok # this returns an empty response with a 200 success status code
    @missed_date = level.missed_dates.new(missed_date_params)
    @missed_date.save
    respond_modal_with @date_missed, location: root_path # How to intergrate with this line?
  end

这是因为错误:

AbstractController::DoubleRenderError (Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at most once per action. Also note that neither redirect nor render terminate execution of the action, so if you want to exit an action after redirecting, you need to do something like "redirect_to(...) and return".):
  app/controllers/missed_dates_controller.rb:26:in `create'

现在我知道了原因,即我在 form_for url: habit_level_missed_dates_path({ habit_id: @habit, level_id: @habit.current_habit_level.id }) 中有,但我需要在那里,至少我认为是这样,以便在创建时传递这些属性。

我该如何修改这一点,以便当用户在@missed_date 表单中单击提交时,id 的通行证并将他重定向回root_url

我按照本教程学习模态逻辑:http://www.jetthoughts.com/blog/tech/2014/08/27/5-steps-to-add-remote-modals-to-your-rails-app.html

【问题讨论】:

    标签: ruby-on-rails ruby model-view-controller


    【解决方案1】:

    您正在调用head,它返回一个仅标头响应see Using head To Build Header-Only Responses on Rails Guides。调用head 相当于渲染块中的​​return 语句。看起来您正在调用head,然后继续渲染更多代码,因此出现双重渲染错误。您可能根本不需要该行,因为默认响应代码无论如何都是 200。

    【讨论】:

    • 非常感谢!太完美了。
    猜你喜欢
    • 2014-07-02
    • 1970-01-01
    • 2016-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-26
    相关资源
    最近更新 更多