AnthonyGalli.com
AnthonyGalli.com

Reputation: 2866

AbstractController::DoubleRenderError

When I submit this:

<%= 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| %>

The modal doesn't disappear because the page doesn't redirect_to :root_url as it's suppose to with the create action.

  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

This is because of the error:

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'

Now I know the cause, which is that I have in the form_for url: habit_level_missed_dates_path({ habit_id: @habit, level_id: @habit.current_habit_level.id }), but I need that there, at least I think so, to pass those attributes upon creation.

How can I revise this so that when the user clicks submit in the @missed_date form the id's pass AND he is redirected back to the root_url?

I followed this tutorial for the modal logic: http://www.jetthoughts.com/blog/tech/2014/08/27/5-steps-to-add-remote-modals-to-your-rails-app.html.

Upvotes: 0

Views: 224

Answers (1)

Ryenski
Ryenski

Reputation: 9692

You are calling head, which returns a header-only response see Using head To Build Header-Only Responses on Rails Guides. Calling head is the equivalent of a return statement in the render block. It looks like you're calling head, and then going on to render more code, hence the double-render error. You probably don't need that line at all because the default response code is 200 anyway.

Upvotes: 2

Related Questions