Rails - 如何显示数据库中的数据
我对 Rails 相当陌生,我只是想在页面上显示 sqlite3 数据库中记录的 user_id 。这是我的代码:
模型
class User < ActiveRecord::Base
set_primary_key :user_id
has_one :user_id
end
控制器
class HomeController < ApplicationController
def index
@users = User.find(0)
end
end
视图
<%= User.users.user_id %>
我确信我犯了一个愚蠢的错误。
Im fairly new to Rails and i am simply trying to display the user_id of a record in a sqlite3 database on a page. Here is my code:
Model
class User < ActiveRecord::Base
set_primary_key :user_id
has_one :user_id
end
Controller
class HomeController < ApplicationController
def index
@users = User.find(0)
end
end
View
<%= User.users.user_id %>
Im sure im making a silly mistake.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
或者在您看来:
这将打印出 User 中所有记录的 user_id
Alternatively in your view:
This will print out the user_id for all the records in User
如果您只需要用户对象的 UID,那么让 Rails 为您解决这个问题是相当标准的,因此您只需生成表所需的属性,而不包含 user_id。不过,您可能需要这个 user_id,在这种情况下请忽略我刚才所说的内容。
你不需要 has_one 关系,除非你有另一个名为 user_id 的模型(我猜这是可能的)
索引方法通常意味着返回所有用户,或者至少一个子集(例如,如果你使用分页),因此我建议在索引方法中使用以下内容
请注意,我已经去掉了括号,它没有什么区别,但它是一个 Rails 约定(它使它看起来更像是一个 DSL)。
要返回 1 个用户,您可能需要这样的方法
,但是约定规定 id 将由发送的 url 指定,因此
如果您随后访问像 show 方法上面指定的 url (/posts/1,其中 1 是 ID)
此方法的视图应如下所示:
请注意,@user 是由 show 方法传递给视图的成员变量,在本例中是 id 为 1 的用户的实例,并且您是访问该实例的 user_id 属性,因此您不需要
User.
因为这将调用类方法而不是用户对象实例的方法。要回答您的具体问题,它应该看起来更像
模型
控制器
使用 /users/0 访问显示视图
该视图将包含
编辑:
要显示登录的用户,您可能需要一个用户会话对象,该对象是在用户登录时创建的,并且当用户注销时被销毁。用户会话对象应该有一个关联的用户对象,然后您可以查询记录用户的 user_session 对象。
我强烈建议使用 authlogic,它很简单,可以知道发生了什么,但确实有效
If you only want a UID for a user object it's fairly standard to let rails sort this out for you, so you just generate the required attributes of your table and don't include user_id. However it might be the case you want this user_id in which case ignore what I just said.
You don't need the has_one relation unless you have another model named user_id (which is possible I guess)
The index method is generally meant to return all users, or at least a subset (if you use pagination for example) I would therefore recommend using the following in an index method
Notice I have left the brackets off, it doesn't make a difference but is a rails convention (it make it looks more like a DSL).
To return 1 user you might want a method like this
But convention states the id would be specified by url sent, so it might look more like this
if you then access a url like the one specified above the show method (/posts/1, where 1 is the ID)
The view for this method should look something like this:
Note the @user is a member variable passed to the view by the show method, in this case an instance of a user with the id 1, and you are accessing the user_id attribute of that instance so you don't need the
User.
as this would be calling on a class method not a method of an instance of the user object.To answer your specific question it should look more like
Model
Controller
Accessing the show view with /users/0
The View would contain
EDIT:
To display the user logged in you will probably need a user session object which is created when a user logs in and destroyed when the user logs out. The user session object should have an associated user object, you can then query the user_session object for which user is logged.
I strongly recommend using authlogic, it's simple enough to know whats going on but does the trick
打印整个数据库的使用;
例如
To print the whole DB use;
e.g
您确定这里的
User.find(0)
吗?ID
不是以 1 开头吗?为了安全起见,请执行视图中的操作,您会这样做
Are you sure about the
User.find(0)
here? Doesn't theID
start with one? To be safe, doThe in the view, you would do