![](/img/trans.png)
[英]Why controller action should call one model method other than an initial find or new?
[英]controller action contains more than one model method call?
我在Rails 4
控制器中有一個簡單的update
操作:
#more stuff here
def update
if @user.update(user_params)
flash[:notice] = "User #{@user.username} updated"
redirect_to users_path
else
render 'edit'
end
end
private
def set_user
@user = User.find(params[:id])
end
但是,RubyMine警告@user.update
和@user.username
:
此檢查警告在初始.find或.new之后,如果控制器動作包含多個模型方法調用。 建議您在模型類中實現所有業務邏輯,並使用單個方法進行訪問。
我在這里看more than one model method call
。 有人可以解釋發生了什么嗎?
編輯 -我在create
操作中有類似的內容而沒有警告,因此我相信與user_params
有user_params
...
def create
if @user.save
flash[:notice] = "User #{@user.username} created"
redirect_to users_path
else
render 'new'
end
end
假設username
是模型中合並用戶first_name
和last_name
。
我猜@user.update(user_params)
和@user.username
都是您的兩個方法調用。 一個保存模型,另一個保存快速通知中的用戶全名。
這只是來自紅寶石的警告,它僅建議您執行一些操作,而不必遵循這些操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.