我无法理解并解决这个错误.plz帮帮我。 下面是我的错误。
ProductsController中的NoMethodError#创建未定义的方法`产品'对于nil:NilClass(来自productscontroller' s" @product = @ user.products.build(products_params)&#34)
class ProductsController < ApplicationController
before_action :signed_in_user,only:[:new,:create]
before_action :find_user_object,except:[:create]
def index
@products = @user.products.all
end
def show
@product = @user.product.build.find(params[:id])
end
def new
@user = User.find(params[:user_id])
# => 多分before action化させる方が良い
#urlでproducts/newなっててUserのidが取れてない。
redirect_to signin_url, notice:"U have to sign in to publish your furniture." unless sign_in @user
@product = Product.new
end
def create
@product = @user.products.build(products_params)
if @product.save
flash[:success] = "You could add new item:)"
redirect_to @user #後にaction: :indexに変更したい
else
flash.now[:error] = "You couldn't add an item."
render 'new'
end
end
def edit
end
def update
if @product.update_attributes(products_params)
flash[:success] = "You updated your product info"
redirect_to @products
else
flash.now[:error] = "couldn't update :("
redirect_to products_edit_path
end
end
def destroy
#あった方がいいかもしれない@user = Product.find(params[:id])
@product.destroy
redirect_to root_url
end
private
def products_params
params.require(:product).permit(:id,:name,:kind,:size,:discription,:price)
end
#before_action
def signed_in_user
redirect_to signin_url, notice:"Please sign in." if signed_in?
end
def find_user_object
@user = User.find_by(params[:user_id])
end
end
以上是productscontroller.below是用户控制器。
class UsersController < ApplicationController
include UsersHelper
before_action :signed_in_user,only:[:edit,:update]
before_action :correct_user,only:[:edit,:update]
def show
@user = User.find(params[:id])
sign_in @user if signed_in?
end
def new
@user = User.new
end
def create
@user = User.new(user_params)
if @user.save!
sign_in @user
flash[:success] = "Success creating user"
redirect_to @user
else
flash.now[:error] = "couldn't create...."
render 'new'
end
end
def edit
end
def update
if @user.update_attributes(user_params)
flash[:success] = "User info was updated"
redirect_to user_url
else
flash.now[:error] = "You could not edit your profile"
render 'edit'
end
end
def destroy
#あった方がいいかもしれない@user = Product.find(params[:id])
@user.destroy
redirect_to root_url
end
private
def user_params
params.require(:user).permit(:id,:user_id,:name,:email,:username,:status,:from,:when,:password,:password_confirmation)
end
#before_aciton
def correct_user
@user = User.find(params[:id])
redirect_to signin_url,notice:"You have to sign in to edit your profile." unless current_user=(@user)
end
def signed_in_user
redirect_to signin_url, notice:"Please sign in." if signed_in?
end
end
以下是routes.rb。
KaguShop::Application.routes.draw do
resources :users,only:[:show,:new,:create,:edit,:update,:destroy] do
resources :products,only:[:index,:new,:create,:destroy,:show,:new,:edit,:update]
end
resources :sessions,only:[:new,:create,:destroy]
resources :carts,only:[:new,:create,:destroy]#,:showに関しては恐らくいらない。newで既にオブジェクトも作る
root 'products#index'
match '/signup', to:'users#new',via:'get'
match '/signin', to:'sessions#new', via:'get'
match '/signout', to:'sessions#destroy', via:'delete'
match '/contact', to:'nomal_pages#contact', via:'get'
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
# root 'welcome#index'
# Example of regular route:
# get 'products/:id' => 'catalog#view'
# Example of named route that can be invoked with purchase_url(id: product.id)
# get 'products/:id/purchase' => 'catalog#purchase', as: :purchase
# Example resource route (maps HTTP verbs to controller actions automatically):
# resources :products
# Example resource route with options:
# resources :products do
# member do
# get 'short'
# post 'toggle'
# end
#
# collection do
# get 'sold'
# end
# end
# Example resource route with sub-resources:
# resources :products do
# resources :comments, :sales
# resource :seller
# end
# Example resource route with more complex sub-resources:
# resources :products do
# resources :comments
# resources :sales do
# get 'recent', on: :collection
# end
# end
# Example resource route with concerns:
# concern :toggleable do
# post 'toggle'
# end
# resources :posts, concerns: :toggleable
# resources :photos, concerns: :toggleable
# Example resource route within a namespace:
# namespace :admin do
# # Directs /admin/products/* to Admin::ProductsController
# # (app/controllers/admin/products_controller.rb)
# resources :products
# end
end
并且,产品型号有.... ①name②size③kind④dcription⑤价格(当然id但没有product_id)
用户模型...... ①name②email③username④password⑤password_confirmation⑥from⑦when⑧status
答案 0 :(得分:2)
你有before_action :find_user_object,except:[:create]
。这意味着除了@user
之外的所有操作都会设置create
。但您需要在@user
操作中设置create
。所以你应该删除except:[:create]
并离开:
before_action :find_user_object
答案 1 :(得分:1)
在ProductsController
你有
before_action :find_user_object,except:[:create]
因此,@user
操作中未设置create
因为例外。
答案 2 :(得分:1)
在show
方法中,您build
然后find
在show
方法下面看它应该是这样的
请检查user_id
表中是否还有products
class ProductsController < ApplicationController
before_action :find_user_object
def show
@product = @user.products.find(params[:id])
end
end