ruby on Rails 3 中的数据库只能从 user_controller 访问吗?

发布于 2024-11-30 17:20:40 字数 589 浏览 1 评论 0原文

是否可以从任何控制器中提取用户对象?对我来说,它似乎只适用于我的用户控制器。

我正在尝试呈现一个表单,用户可以使用该表单来更新他们在网站上注册的详细信息,例如姓名、电子邮件、密码等。问题是页面无法加载(使用 .load jquery 函数调用它) )。当表单添加到页面时,它不会加载,但当表单不存在时,它会加载。我认为这与无法访问数据库有关。

在我的页面控制器中我有这个:

def edit_account

    @user  = User.find(params[:id])
    case params[:view]
      when 'basic_info'
        render 'pages/settings/basic_info'
      when 'relatives'
        render 'pages/settings/relatives'
    end

    @title = "My Account"
  end

它似乎不起作用。我错过了什么吗? @user 对象在 user_controller 及其关联视图中工作正常,但在页面控制器中则不然。有没有办法授予页面控制器访问权限?

谢谢

Is it possible to pull a user object from any controller? For me it seems to only work in my users controller.

I'm trying to render a form that a user can use to update the details they signed up to the website with e.g. names, email, password etc. The problem is the page won't load (it is called using .load jquery function). It won't load when the form is added to the page but will load when it isn't there. I'm thinking it's something to do with it not being able to access the database.

In my pages controller I have this:

def edit_account

    @user  = User.find(params[:id])
    case params[:view]
      when 'basic_info'
        render 'pages/settings/basic_info'
      when 'relatives'
        render 'pages/settings/relatives'
    end

    @title = "My Account"
  end

It doesn't seem to work. Am I missing something?
@user object works fine in user_controller and it's associated views but not in pages controller. Is there a way to give pages controller access?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眸中客 2024-12-07 17:20:40

如果您使用正确的 REST 方法,则 id 将是页面的 id,而不是用户的 id。

你可以做的是

@page = Page.find(params[:id]) 

如果页面和用户相关(即页面属于用户),你可以这样做:

@user = @page.user

If you are using proper REST methods, the id will be the id of the page not the id of the user.

What you can do is

@page = Page.find(params[:id]) 

And if pages and users are related (ie a page belongs to a user), you can do:

@user = @page.user
风铃鹿 2024-12-07 17:20:40

您可以从任何控制器访问User

但是,您查找用户的方式会有所不同,具体取决于您可以传递给 User.findparams

You can access User from any controller.

However the way you find the user will differ depending on what params you have available to pass to User.find

指尖微凉心微凉 2024-12-07 17:20:40

问题是您在 UsersController 之外的控制器中使用了错误的参数键。在其他控制器中,参数将为user_id。所以...

@user = User.find(params[:user_id])

就像关于代码其余部分的注释一样。

这里视图的使用是错误的。为您的视图创建单独的操作(如果您不想做到 RESTful)。当视图在单个方法内处理时,维护起来会更加困难。更好的说法是:

def basic_info
  view_defaults
end

def relatives
  view_defaults
end

private 

def view_defaults
    @user  = User.find(params[:user_id])
    @title = "My Account"
end

并设置正确的路线来连接这些动作。

The problem is that you are using the wrong params key in controller other than UsersController. In other controllers the params would be user_id. So...

@user = User.find(params[:user_id])

Just as a note about the rest of the code.

The use of view is just wrong here. Create separate actions for your views (if you aren't trying to be RESTful). It is harder to maintain when the view are handled inside a single method. It would be better to say:

def basic_info
  view_defaults
end

def relatives
  view_defaults
end

private 

def view_defaults
    @user  = User.find(params[:user_id])
    @title = "My Account"
end

And set the proper routes to connect these actions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文