ruby on Rails 3 中的数据库只能从 user_controller 访问吗?
是否可以从任何控制器中提取用户对象?对我来说,它似乎只适用于我的用户控制器。
我正在尝试呈现一个表单,用户可以使用该表单来更新他们在网站上注册的详细信息,例如姓名、电子邮件、密码等。问题是页面无法加载(使用 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您使用正确的 REST 方法,则 id 将是页面的 id,而不是用户的 id。
你可以做的是
如果页面和用户相关(即页面属于用户),你可以这样做:
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
And if pages and users are related (ie a page belongs to a user), you can do:
您可以从任何控制器访问
User
。但是,您查找用户的方式会有所不同,具体取决于您可以传递给
User.find
的params
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 toUser.find
问题是您在 UsersController 之外的控制器中使用了错误的参数键。在其他控制器中,参数将为
user_id
。所以...就像关于代码其余部分的注释一样。
这里视图的使用是错误的。为您的视图创建单独的操作(如果您不想做到 RESTful)。当视图在单个方法内处理时,维护起来会更加困难。更好的说法是:
并设置正确的路线来连接这些动作。
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...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:
And set the proper routes to connect these actions.