简体   繁体   English

控制器动作包含多个模型方法调用?

[英]controller action contains more than one model method call?

I have a simple update action in a Rails 4 controller: 我在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

However, RubyMine is warning about @user.update and @user.username : 但是,RubyMine警告@user.update@user.username

This inspection warns if a controller action contains more than one model method call, after the initial .find or .new. 此检查警告在初始.find或.new之后,如果控制器动作包含多个模型方法调用。 It's recommended that you implement all business logic inside the model class, and use a single method to access it. 建议您在模型类中实现所有业务逻辑,并使用单个方法进行访问。

I don't see more than one model method call here. 我在这里看more than one model method call Can some one explain what is going on? 有人可以解释发生了什么吗?

EDIT - I have something similar in the create action without warns, so I believe there is something to do with user_params ... 编辑 -我在create操作中有类似的内容而没有警告,因此我相信与user_paramsuser_params ...

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

Assuming username is a method in model where you merge user first_name and last_name . 假设username是模型中合并用户first_namelast_name

I guess @user.update(user_params) and @user.username are your both method calls. 我猜@user.update(user_params)@user.username都是您的两个方法调用。 One that saves the model, another that sets the user full name in flash notice. 一个保存模型,另一个保存快速通知中的用户全名。

It's just a warning from rubymine that just recommends you some actions to do, not necessary to follow them. 这只是来自红宝石的警告,它仅建议您执行一些操作,而不必遵循这些操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM