![](/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.