用户has_many评论关联

时间:2016-06-12 22:14:33

标签: ruby-on-rails

我试图为用户创建一种审核其他用户的方法。

当我尝试访问www.site.com/users/1/reviews/new时,我会被重定向到我的主页,其中找不到资源'。我猜这与我的路线有关吗?

的routes.rb

$name

#categories associatoin   资源:类别,除了:[:destroy]

root' home #index'   获取'个人资料'到:'用户#show'

user.rb

Rails.application.routes.draw do
  devise_for :users,
    :controllers => { registrations: 'registrations',
                      omniauth_callbacks:  'omniauth_callbacks'} #<-- that thing is for STRIPE!
  #STRIPE
  resources :charges
  #MAILBOXER
  resources :conversations, only: [:index, :show, :destroy] do
      member do
        post :reply
        post :restore
        post :mark_as_read
      end
      collection do
        delete :empty_trash
      end
  end
  resources :messages, only: [:new, :create]

  #user reviews (THE PROBLEM)
  resources :users do
    resources :reviews
  end

review.rb

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable,
         :omniauthable
  #Categories association stuff
  has_many :user_categories
  has_many :categories, through: :user_categories

  #user review association stuff 
  has_many :reviews


  acts_as_messageable

  def mailboxer_email(object)
    email
  end
end

reviews_controller.rb

class Review < ActiveRecord::Base
    belongs_to :user
end

1 个答案:

答案 0 :(得分:0)

def set_user
  @user = User.find(params[:id])
end

应该是:

def set_user
  @user = User.find(params[:user_id])
end