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

我对 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 = current_user.favorite_user # problematic!
  @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

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

NoMethodError in ToolsController#index
undefined method `favorite_user' for #<User:0x39df638>

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

路线

Rails.application.routes.draw do
  devise_for :users

  get 'welcome/index'
  resources :tools do
    member do 
      get "like", to: "tools#upvote"
      get "dislike", to: "tools#downvote"
    end
    get :favorite, on: :member
  end

  resources :users, only: [:index, :show] do 
    get :userfavorite, on: :member
  end

  authenticated :user do
    root 'tools#index', as: "authenticated_root"
  end

  root 'welcome#index'

  get '/search' => 'tools#search'

  get '/users' => 'users#index'
end

【问题讨论】:

  • 我不得不说,有两个同名的变量user@user 非常令人困惑。我会尽量避免这种情况,这样可以避免很多麻烦。
  • 顺便说一句,我想你并不是真的想写@userfavorite_user_path而是userfavorite_user_path
  • 你是对的!我只是专注于 @User 让它出现 - 谢谢!

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


【解决方案1】:

改变这个:

@user = current_user.favorite_user

到:

@favorite_users = current_user.favorite_users

userhas_many favorite_users

不要忘记在调用 @user 的地方重命名实例变量。

【讨论】:

  • 这行得通(至少在控制器中//不要在那里给出错误)。但我不能选择像用户名这样的属性,例如这个解决方案
  • 这是一个集合,你必须循环思考它
  • 这就是我正在做的事情,但我还想访问该循环中用户项的某些属性(用户名)
  • app/views/tools/index.html 我想输出用户的用户名:@favorite_users.username@favorite_users.user.username 都不起作用...跨度>
【解决方案2】:

这就是解决方案!

@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
    相关资源
    最近更新 更多