2014-09-02 72 views
0

我不能理解和解決這個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 ④密碼⑤密碼確認⑥從⑦時間⑧狀態

回答

1

ProductsController你有

before_action :find_user_object,except:[:create] 

因此沒有@user被在create動作設置,因爲異常。

+0

我犯了這個錯誤,但仍然錯誤。 – user3674902 2014-09-02 14:14:27

+0

** ActiveRecord :: UnknownAttributeError ProductsController#create 未知屬性:user_id ** – user3674902 2014-09-02 14:14:46

+0

你能解決這個問題嗎? – user3674902 2014-09-02 14:15:25

2

您有before_action :find_user_object,except:[:create]。這意味着@user被設置爲除create之外的所有操作。但您確實需要@user才能設置爲create操作。所以,你應該只是刪除except:[:create]離開:

before_action :find_user_object 
+0

我犯了那個錯誤,但仍然水道錯誤。 **產品控制器中的ActiveRecord :: UnknownAttributeError#創建未知屬性:user_id ** 你能解決這個問題嗎? – user3674902 2014-09-02 14:16:01

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 
相關問題