Pergunta

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
Foi útil?

Solução

Move find to private method:

before_action :find_admin, only: [:update]

private

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

Outras dicas

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top