ruby-on-rails – 为什么渲染方法会在编辑后更改单个资源的路径
发布时间:2020-12-17 01:58:26 所属栏目:百科 来源:网络整理
导读:好的,所以我有一个用户has_one模板,我想要一个页面,基本上只是模板的编辑视图. 我有: class TemplatesController ApplicationController def edit @template = current_user.template end def update @template = current_user.template if @template.updat
好的,所以我有一个用户has_one模板,我想要一个页面,基本上只是模板的编辑视图.
我有: class TemplatesController < ApplicationController def edit @template = current_user.template end def update @template = current_user.template if @template.update_attributes(params[:template]) flash[:notice] = "Template was successfully updated" end render :edit end 结束 现在’问题’就是我调用render:edit我实际上最终在/template.1而不是/ template / edit这是我所期望的.显然,如果我调用redirect_to:edit然后我会得到我期望的路径但是如果有的话我会丢失对象错误. 有一个更好的方法吗? 谢谢!! 解决方法
通常在编辑/更新操作对中,只有在出现错误时才会从更新中重新呈现编辑,相应地设置闪存.如果你已经在模板/ 1 /编辑(这是我期望的),那么网址逻辑上不会改变,因为你告诉浏览器只是渲染你发送它的文本.这是预期的行为.如果您成功更新,那么您可以重定向到显示或索引或者您需要从哪里开始,并且闪存将保留通知文本(这是闪存的用途),即使模型不会.请注意,对于渲染操作,您需要使用Flash.now,以便消息不会在下一次重定向时保留.
def update @template = current_user.template if @template.update_attributes(params[:template]) flash[:notice] = "Template was successfully updated" redirect_to(@template) else flash.now[:error] = @template.errors[:base] render :edit end end (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |