0
我正試圖爲用戶創建一種方式來檢查其他用戶。用戶has_many評論協會
當我嘗試進入www.site.com/users/1/reviews/new時,我被重定向到我的主頁上,找不到資源。我猜它與我的路線有關?
的routes.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
#categories associatoin 資源:類別,除了:[:摧毀]
根的家#指數「 獲得 '輪廓',以:「用戶#秀'
端
user.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
review.rb
class Review < ActiveRecord::Base
belongs_to :user
end
reviews_controller.rb
class ReviewsController < ApplicationController
before_action :set_review, only: [:show, :edit, :update, :destroy]
before_action :set_user
before_action :authenticate_user!
# GET /reviews
# GET /reviews.json
def index
@reviews = Review.all
end
# GET /reviews/1
# GET /reviews/1.json
def show
end
# GET /reviews/new
def new
@review = Review.new
end
# GET /reviews/1/edit
def edit
end
# POST /reviews
# POST /reviews.json
def create
@review = Review.new(review_params)
@review.user_id = current_user.id
@review.user_id = @user.id
end
# PATCH/PUT /reviews/1
# PATCH/PUT /reviews/1.json
def update
respond_to do |format|
if @review.update(review_params)
format.html { redirect_to @review, notice: 'Review was successfully updated.' }
format.json { render :show, status: :ok, location: @review }
else
format.html { render :edit }
format.json { render json: @review.errors, status: :unprocessable_entity }
end
end
end
# DELETE /reviews/1
# DELETE /reviews/1.json
def destroy
@review.destroy
respond_to do |format|
format.html { redirect_to reviews_url, notice: 'Review was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_review
@review = Review.find(params[:id])
end
def set_user
@user = User.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def review_params
params.require(:review).permit(:rating, :comment)
end
end
我不能立即看到錯誤的原因,但有幾件事我可以指出。你的'set_user'方法應該使用'params [:user_id]'。另外你的創建動作,似乎是設置'user_id'兩次。我想你可能想要使用current_user或@user – oreoluwa