Why does the rendering method change the path for a unique resource after editing?

OK, so I have a User who has_one the Template , and I need a page, which basically is just a view of the Template view .

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 arises when I call render: edit I actually end up with /template.1 instead of / template / edit, as expected. Obviously, if I call redirect_to: edit, I would get the path that I expect, but I would lose the object errors if they were.

Is there a better way to do this?

Thank!!

+3
source share
3

/ , , . /1/edit ( , ), , , . . , , , ( ), . , 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
+2

, . . Rails Guides.

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

in my opinion u can not loose Template object (in the editing action) because you get it from the user model

render edit_template_patch(@template)

and you get template/:id/editfor example.template/1/edit

-2
source

Source: https://habr.com/ru/post/1780959/


All Articles