has_many 关联轨道

发布于 2024-11-06 01:34:51 字数 893 浏览 3 评论 0原文

我有一个用户模型和一个 Job_Category 模型,Job_Category 模型

belongs_to :user

用户模型

has_many :job_categories, :dependent => :destroy   

我有一个仪表板控制器,并试图显示特定登录用户的所有 Job_Categories。

class DashboardController < ApplicationController
before_filter :authenticate_user!


def index
 @user = User.find(params[:id])
 @job_categories = @user.job_categories


 #@job_categories = JobCategory.all

 #respond_to do |format|
#   format.html # index.html.erb
#   format.xml  { render :xml => @job_categories }
# end

end

但是,当我尝试显示此内容时,出现错误“无法找到没有 ID 的用户”。我在日志中看到这一点:

Processing by DashboardController#index as HTML 用户负载 (0.3ms) SELECT "users".* FROM "users" WHERE ("users"."id" = 2) LIMIT 1 在 22 毫秒内完成

ActiveRecord::RecordNotFound(无法找到没有 ID 的用户): 应用程序/控制器/dashboard_controller.rb:9:在“索引”中

I have a User model and a Job_Category model, the Job_Category model

belongs_to :user

The User model

has_many :job_categories, :dependent => :destroy   

I have a Dashboard Controller and am trying to display all the Job_Categories for a specific logged in User.

class DashboardController < ApplicationController
before_filter :authenticate_user!


def index
 @user = User.find(params[:id])
 @job_categories = @user.job_categories


 #@job_categories = JobCategory.all

 #respond_to do |format|
#   format.html # index.html.erb
#   format.xml  { render :xml => @job_categories }
# end

end

However, when I am trying to display this I get the error 'Couldn't find User without an ID' . I see this in the log:

Processing by DashboardController#index as HTML
User Load (0.3ms) SELECT "users".* FROM "users" WHERE ("users"."id" = 2) LIMIT 1
Completed in 22ms

ActiveRecord::RecordNotFound (Couldn't find User without an ID):
app/controllers/dashboard_controller.rb:9:in `index'

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

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

发布评论

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

评论(1

百合的盛世恋 2024-11-13 01:34:51

首先,你不能尝试在控制台中通过参数查找,因为不存在这样的东西。

在控制台中尝试这个:

用户.find(2)

(..default 列,find 方法看起来的值是主键,我假设它是 id )

然后另一件事,如果你已经在 before_filter 中验证用户,你可能已经有 @current_user 或其他东西设置,您是否使用一些身份验证插件,例如 devise 之类的?因此无需将当前用户 ID 传递给仪表板控制器。应用程序控制器已经知道这个用户是谁,并且您的仪表板控制器是它的子类。

并确保您的 job_category 表也有 user_id 列。

First of all, you cannot try to find by params in console, because there ain't such thing.

Try this in console:

User.find(2)

(..default column where find method looks the value is primary key which I assume is id as usual)

Then another thing, if you're already authenticating user in before_filter, you might already have @current_user or something set, are you using some authentication plugin like devise or something? So there's no need to pass current users id to dashboard controller. Application controller already knows who this user is and your dashboard controller is a subclass of it.

And ensure your job_category table has user_id column too.

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