gpt4 book ai didi

ruby-on-rails - 如何在RoR中的不同 Controller 中使用模型

转载 作者:行者123 更新时间:2023-12-03 16:12:07 24 4
gpt4 key购买 nike

我有一个配置文件 Controller 。但是我想使用User模型在users表中保存一些字段。那么如何将用户模型加载到概要文件 Controller 中?

最佳答案

模型是独立的实体,您可以从任何 Controller 调用它。

There can be a model without a controller and vice-versa.

对于您的问题,请在下面举一个例子
class ProfileController < ApplicationController

def some_method
@user = User.find(params[:user_id])
if @user.update_attributes(params[:user])
// some action
else

// some action
end
end

end

关于ruby-on-rails - 如何在RoR中的不同 Controller 中使用模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3005002/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com