簡體   English   中英

控制器動作包含多個模型方法調用?

[英]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_paramsuser_params ...

def create
  if @user.save
    flash[:notice] = "User #{@user.username} created"
    redirect_to users_path
  else
    render 'new'
  end
end

假設username是模型中合並用戶first_namelast_name

我猜@user.update(user_params)@user.username都是您的兩個方法調用。 一個保存模型,另一個保存快速通知中的用戶全名。

這只是來自紅寶石的警告,它僅建議您執行一些操作,而不必遵循這些操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM