【问题标题】:Rails 4 Error: ActiveRecord::RecordNotFound Couldn't find 'id'=Rails 4 错误:ActiveRecord::RecordNotFound 找不到 'id'=
【发布时间】:2016-09-06 15:04:38
【问题描述】:

我对 Rails 和编写我的第一个应用程序还很陌生。只是不知道如何定位 current_user 最喜欢的用户(三个模型:用户、工具、FavoriteUser)。

控制器(工具)

def index
  @favorites = current_user.favorites.order("created_at DESC")
  @userfavorites = current_user.userfavorites.order("created_at DESC")
  @tools = Tool.where(user_id: current_user).order("created_at DESC")
  @user = FavoriteUser.find(params[:c_user_id]).user_id
  @cuser = current_user
end

索引视图(工具)

%h2 My Favorite Users
- @userfavorites.each do |user|
    = image_tag gravatar_for @user if @user.use_gravatar == true
    = image_tag @user.avatar_filename.url if @user.use_gravatar == false
    %h2= link_to @user.username, @user
    %p= link_to "Favorite", @userfavorite_user_path(user, type: "favorite"), method: :get
    %p= link_to "Unfavorite", @userfavorite_user_path(user, type: "unfavorite"), method: :get

如果我在浏览器中运行它会出现以下错误:

ActiveRecord::RecordNotFound in ToolsController#index
Couldn't find FavoriteUser with 'id'=

编辑:

FavoriteUser 数据库:

user_id:integer #favorited
c_user_id:integer #active user

我就是想不通。

提前感谢您的帮助!

增加的关系:

user.rb

has_many :favorite_users # just the 'relationships'
  # Favorite users of user
  has_many :favorite_relationships, class_name: "FavoriteUser", foreign_key: "c_user_id"
  has_many :userfavorites, through: :favorite_relationships, source: :user

  # Favorited by a user
  has_many :favorited_relationships, class_name: "FavoriteUser", foreign_key: "user_id"
  has_many :userfavorited_by, through: :favorited_relationships, source: :c_user

favorite_user.rb

class FavoriteUser < ActiveRecord::Base
    belongs_to :c_user, class_name: "User"
    belongs_to :user, class_name: "User"
end

【问题讨论】:

  • 它说,params[:c_user_id] 在您的控制器操作中是 nil。您可以从即将执行的操作的日志中复制并粘贴 params 吗?
  • @RAJ 怎么可能是零?将这些参数添加到问题中
  • 我假设您在浏览器中点击/tools URL,其中c_user_id 参数不存在,这就是params[:c_user_id] 为零的原因。你可以从日志中粘贴参数吗?还要确认您点击的是哪个 URL。
  • 我点击了 localhost:3000。这是你需要的吗? irb(main):004:0> FavoriteUser.first FavoriteUser Load (0.0ms) SELECT "favorite_users".* FROM "favorite_users" ORDER BY "favorite_users"."id" ASC LIMIT 1 => # irb(main):005:0>

标签: ruby-on-rails ruby ruby-on-rails-4 activerecord controller


【解决方案1】:

如果您正确设置了关系,以下解决方案应该适合您。

替换:

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

# Returning complete user object instead of only user's id,
# as it's getting used on the view
@user = current_user.favorite_user

【讨论】:

  • 关系正常(在控制台中测试)但对我来说似乎你不能做你所做的。我得到一个未定义的方法错误
  • 我会很快将关系添加到问题中
【解决方案2】:
params[:c_user_id]

在调用此控制器操作时为零。如果你想访问它,你必须发送它。如果您将此行放在控制器操作的顶部

puts "#{params[:c_user_id]}"

你会看到 params[:c_user_id] 为 nil。

【讨论】:

  • 我到底是怎么做到的?在模型中?我已经在用户模型中做到了这一点:has_many :favorite_users # just the 'relationships' # Favorite users of user has_many :favorite_relationships, class_name: "FavoriteUser", foreign_key: "c_user_id" has_many :userfavorites, through: :favorite_relationships, source: :user # Favoritad by a user has_many :favorited_relationships, class_name: "FavoriteUser", foreign_key: "user_id" has_many :userfavorited_by, through: :favorited_relationships, source: :c_user
【解决方案3】:

这就是解决方案!

@userfavorites = current_user.userfavorites

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多