一个AbstractController :: DoubleRenderError

时间:2015-09-02 02:36:22

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

当我提交时:

<%= 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表单中的提交时,我会将其重定向回root_url

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

1 个答案:

答案 0 :(得分:2)

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