Rails - 如何显示数据库中的数据

发布于 2024-10-24 08:47:30 字数 418 浏览 4 评论 0原文

我对 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 技术交流群。

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

发布评论

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

评论(4

哑剧 2024-10-31 08:47:30

或者在您看来:

<% User.all.each do |user| %>
  <%= user.id %>
<% end %>

这将打印出 User 中所有记录的 user_id

Alternatively in your view:

<% User.all.each do |user| %>
  <%= user.id %>
<% end %>

This will print out the user_id for all the records in User

二货你真萌 2024-10-31 08:47:30

如果您只需要用户对象的 UID,那么让 Rails 为您解决这个问题是相当标准的,因此您只需生成表所需的属性,而不包含 user_id。不过,您可能需要这个 user_id,在这种情况下请忽略我刚才所说的内容。
你不需要 has_one 关系,除非你有另一个名为 user_id 的模型(我猜这是可能的)

索引方法通常意味着返回所有用户,或者至少一个子集(例如,如果你使用分页),因此我建议在索引方法中使用以下内容

User.find :all  

请注意,我已经去掉了括号,它没有什么区别,但它是一个 Rails 约定(它使它看起来更像是一个 DSL)。

要返回 1 个用户,您可能需要这样的方法

# GET /posts/1
def show
   @user = User.first      
end

,但是约定规定 id 将由发送的 url 指定,因此

# GET /posts/1
def show
   @user = User.find params[:id]      
end

如果您随后访问像 show 方法上面指定的 url (/posts/1,其中 1 是 ID)

此方法的视图应如下所示:

<%= @user.user_id %>

请注意,@user 是由 show 方法传递给视图的成员变量,在本例中是 id 为 1 的用户的实例,并且您是访问该实例的 user_id 属性,因此您不需要 User. 因为这将调用类方法而不是用户对象实例的方法。

要回答您的具体问题,它应该看起来更像

模型

class User < ActiveRecord::Base
  set_primary_key :user_id
end

控制器

class HomeController < ApplicationController
  def show
    @user = User.first
  end 
end

使用 /users/0 访问显示视图

该视图将包含

<%= @user.user_id %>

编辑:

要显示登录的用户,您可能需要一个用户会话对象,该对象是在用户登录时创建的,并且当用户注销时被销毁。用户会话对象应该有一个关联的用户对象,然后您可以查询记录用户的 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

User.find :all  

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

# GET /posts/1
def show
   @user = User.first      
end

But convention states the id would be specified by url sent, so it might look more like this

# GET /posts/1
def show
   @user = User.find params[:id]      
end

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:

<%= @user.user_id %>

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

class User < ActiveRecord::Base
  set_primary_key :user_id
end

Controller

class HomeController < ApplicationController
  def show
    @user = User.first
  end 
end

Accessing the show view with /users/0

The View would contain

<%= @user.user_id %>

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

无尽的现实 2024-10-31 08:47:30

打印整个数据库的使用;

Model.all

例如

User.all

To print the whole DB use;

Model.all

e.g

User.all
沉默的熊 2024-10-31 08:47:30

您确定这里的 User.find(0) 吗? ID 不是以 1 开头吗?为了安全起见,请执行

@user = User.first

视图中的操作,您会这样做

<%= @user.user_id %>

Are you sure about the User.find(0) here? Doesn't the ID start with one? To be safe, do

@user = User.first

The in the view, you would do

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