Rails 4错误:ActiveRecord :: RecordNotFound找不到id = [英] Rails 4 Error: ActiveRecord::RecordNotFound Couldn't find id=

查看:216
本文介绍了Rails 4错误:ActiveRecord :: RecordNotFound找不到id =的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是相当新的rails和编码我的第一个应用程序。



控制器(工具)

  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我最喜欢的用户
- @ 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收藏夹,userfavorite_user_path(用户,类型:收藏),方法::get
%p = link_toUnfavorite,userfavorite_user_path(user,type:unfavorite ),方法::get



如果我在浏览器中运行这个错误, >

 在ToolsController中的NoMethodError#index 
未定义的方法`favorite_user'for#< User:0x39df638&

FavoriteUser数据库


$ b b

  user_id:integer #favorited 
c_user_id:integer #active user

我根本无法想出。


预先感谢您的帮助!



关系:

  has_many:favorite_users#只是'关系'
#用户最喜欢的用户
has_many:favorite_relationships,class_name:FavoriteUser,foreign_key:c_user_id
has_many:userfavorites ,通过::favorite_relationships,source::user

#由用户收藏
has_many:favorited_relationships,class_name:FavoriteUser,foreign_key:user_id
has_many:userfavorited_by,通过::favorited_relationships,source::c_user

favorite_user.rb p>

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

ROUTES

  Rails.application。 routes.draw do 
devise_for:users

get'welcome / index'
资源:tools do
成员do
获取like tools#upvote
getdislike,to:tools#downvote
end
get:favorite,on::member
end
$ b b资源: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


解决方案

解决方案!

  @userfavorites = current_user.userfavorites 


I'm fairly new to rails and into coding my first app. Just can't figure out how to target a user the current_user favorited (Three models: User, Tool, FavoriteUser).

Controller (Tools)

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

Index View (Tools)

%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

If I run this in my browser it appears following error:

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

FavoriteUser Database:

user_id:integer #favorited
c_user_id:integer #active user

I simply can't figure it out.

Thanks in advance for your help!

RELATIONSHIPS:

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

ROUTES

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

解决方案

That's the solution!

@userfavorites = current_user.userfavorites

这篇关于Rails 4错误:ActiveRecord :: RecordNotFound找不到id =的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆