Why does the render method change the path for a singular resource after an edit?

跟風遠走 提交于 2019-12-06 02:59:54

问题


OK so I have a User which has_one Template and I want a page which is basically just an edit view of the Template.

I have:

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

end

Now the 'problem' is when I call render :edit I actually end up on /template.1 instead of /template/edit which is what I'd expect. Obviously if I call redirect_to :edit then I'd get the path I expect but I'd loose the object errors if there were any.

Is there a better way to do this?

Thanks!!


回答1:


Normally in an edit/update action pair you would only re-render the edit from update if there were errors, setting the flash accordingly. If you were already on template/1/edit (which is what I would expect), then the url logically won't change since you're telling the browser to simply render the text you're sending it. This is expected behavior. If you were successful in updating then you can redirect to show or index or wherever you need to go from there, and the flash will keep the notice text (that's what the flash is for) even though the model won't. Note that for a render action you need to use Flash.now so that the message won't persist on the next redirect.

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



回答2:


If you're using a singular resource, you want to use resolve in your routes. See this in the Rails Guides.

resource :geocoder
resolve('Geocoder') { [:geocoder] }



回答3:


in my opinion u cant loose Template object (on edit action) because you get it from User model

render edit_template_patch(@template)

and you will get template/:id/edit eg. template/1/edit



来源:https://stackoverflow.com/questions/4475380/why-does-the-render-method-change-the-path-for-a-singular-resource-after-an-edit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!