Reputation:
i'm getting this error for my products and user table.
--Couldn't find user without an id
def set_user
@user = User.find(params[:user_id])
end
I have nested the routes like so..
resources :users do
resources :products do
resources :reviews
end
end
and here is my products controller..
class ProductsController < ApplicationController
before_action :require_signin, except: [:index, :show]
before_action :set_user
def index
@products = @user.products
end
def show
@product = Product.find(params[:id])
end
def edit
@product = Product.find(params[:id])
end
def update
@product = Product.find(params[:id])
if @product.update(product_params)
redirect_to [@user, @product], notice: "Product successfully updated!"
else
render :edit
end
end
def new
@product = @user.products.new
end
def create
@product = @user.products.new(product_params)
@product.user = current_user
if @product.save
redirect_to user_products_path(@product, @user), notice: "Product successfully created!"
else
render :new
end
end
def destroy
@product = Product.find(params[:id])
@product.destroy
redirect_to user_products_path(@product, @user), alert: "Product successfully deleted!"
end
private
def product_params
params.require(:product).permit(:title, :description, :posted_on, :price, :location, :category)
end
def set_user
@user = User.find(params[:user_id])
end
end
All i am trying to do is associate the user and product so the product belongs_to user, and the user has_many products.
class Product < ActiveRecord::Base
belongs_to :user
has_many :reviews
class User < ActiveRecord::Base
has_secure_password
has_many :reviews, dependent: :destroy
has_many :products, dependent: :destroy
Upvotes: 0
Views: 716
Reputation: 899
As other users have mentioned the params[:user_id] value is probably nil.
That said, you already appear to have a current_user defined in the scope of the controller. I see it referenced in the create action. I'd bet that it was set by the require_sign_in before_action. Given what I think you are trying to do, it probably makes your set_user before_action a bit redundant.
You can probably just refer to current_user in your controller anywhere you are currently using @user. Alternatively, you might set @user = current_user in the set_user before_action.
SideNote:
Looking a bit closer at your create action:
def create
@product = @user.products.new(product_params)
@product.user = current_user
if @product.save
redirect_to user_products_path(@product, @user), notice: "Product successfully created!"
else
render :new
end
end
Correct me if I'm wrong but I believe doing something like @model.association.new sets the model_id for the newly created association object so I would change the two lines
@product = @user.products.new(product_params)
@product.user = current_user
to simply be
@product = current_user.products.new(product_params)
Upvotes: 1
Reputation: 4386
For any action of your controller you should pass user_id param. The reason of error is params[:user_id] equal nil
Upvotes: 1