快速问题。一切正常,除了更新主页仪表板上的清单时,它重定向到配置文件页面,因为它使用了配置文件控制器上的更新方法(我认为)。关于如何使它在更新清单时简单地重新加载页面而不改变其内容的任何想法?
这是表格:
<div class="col-md-4 col-xs-12">
<div class="card">
<div class="card-body">
<h3 class="card-title">Onboarding Checklist</h3>
<div class="onboarding-checklist-container">
<%= form_for @profile do |f| %>
<% Checklist.all.each do |item| %>
<%= check_box_tag "profile[checklist_ids][]", item.id, @profile.checklists.include?(item) %>
<%= item.name %><br />
<% end %>
<%= f.submit "Update" %>
<% end %>
<div><!--end onboarding-checklist-container div-->
</div><!--end col card-body div-->
</div><!--end card div-->
</div><!--end col div-->
这是配置文件控制器的更新方法
def update
@profile = current_user.profile
@profile.update_attributes(profile_params)
redirect_to current_profile_path
end
我觉得也许我可以做一个if语句,但是我还不知道如何做。
我还不清楚它在哪里表明这是使用“更新”操作。是吗?
谢谢!
答案 0 :(得分:1)
您可以将附加输入添加到其中一个表单中,如下所示:
<input type="hidden" name="dashboardredirect" value="1"/>
然后在您的控制器中,检查此值是否存在以决定您的重定向:
def update
@profile = current_user.profile
@profile.update_attributes(profile_params)
# Redirect to profile page if no dashboard redirect given,
# Redirect to dashboard if it is given.
redirect_to params[:dashboardredirect].nil? ? current_profile_path : dashboard_path
end
答案 1 :(得分:1)
您可以使用滑轨redirect_back
助手
def update
@profile = current_user.profile
@profile.update_attributes(profile_params)
redirect_back fallback_location: current_profile_path
end
redirect_back
会将您重定向到请求来自服务器的操作,即request.referer
。
对于request.referer为fallback_location
的情况,我们在这里使用nil
。
希望这会有所帮助