Вопрос

RubyMine gives en error about "Each controller action only calls one model method other than an initial find or new" which is related with "Fat Model, Skinny Controller" practice. But I do not know how can I make this code better.

Thanks for your time.

def update
   @admin = Admin.find(params[:id])

   if @admin.update_attributes(permitted_params)
      redirect_to admins_admins_path, notice: "#{@admin.email} updated"
   else
      render action: "edit"
   end
end
Это было полезно?

Решение

Move find to private method:

before_action :find_admin, only: [:update]

private

def find_admin
  @admin = Admin.find params[:id]
end

Другие советы

Rather than having callback, I'll prefer it as method only and have variable @admin set to it. So if it's already set before, no need to hit the db again.

private

def admin
  @admin ||= Admin.find params[:id] if params[:id]
end

In your method you can do:

def update
  if admin.update_attributes(permitted_params)
    redirect_to admins_admins_path, notice: "#{admin.email} updated"
  else
    render action: "edit"
  end
end

This will give you @admin as well.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top