未定义的方法产品为零:NilClass NoMethodError ProductsController#创建
我不能理解和解决这个error.plz帮助我。下面是 是我的错误。未定义的方法产品为零:NilClass NoMethodError ProductsController#创建
NoMethodError中的ProductsController#创建未定义的方法'产品的零:NilClass(从的ProductsController的 “@product = @ user.products.build(products_params)”)
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
below is 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④discription⑤price(当然ID但没有PRODUCT_ID)
和用户模型具有...... ①name②email③username ④密码⑤密码确认⑥从⑦时间⑧状态
在ProductsController
你有
before_action :find_user_object,except:[:create]
因此没有@user
被在create
动作设置,因为异常。
您有before_action :find_user_object,except:[:create]
。这意味着@user
被设置为除create
之外的所有操作。但您确实需要@user
才能设置为create
操作。所以,你应该只是删除except:[:create]
离开:
before_action :find_user_object
我犯了那个错误,但仍然水道错误。 **产品控制器中的ActiveRecord :: UnknownAttributeError#创建未知属性:user_id ** 你能解决这个问题吗? – user3674902 2014-09-02 14:16:01
在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
我犯了这个错误,但仍然错误。 – user3674902 2014-09-02 14:14:27
** ActiveRecord :: UnknownAttributeError ProductsController#create 未知属性:user_id ** – user3674902 2014-09-02 14:14:46
你能解决这个问题吗? – user3674902 2014-09-02 14:15:25