【发布时间】: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