Reputation: 3
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
Upvotes: 0
Views: 725
Reputation: 566
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.
Upvotes: 0
Reputation: 27901
Move find
to private method:
before_action :find_admin, only: [:update]
private
def find_admin
@admin = Admin.find params[:id]
end
Upvotes: 1