Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6031669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:13:07+00:00 2026-05-23T05:13:07+00:00

I followed these questions( one and two ) on how to adjust my methods

  • 0

I followed these questions(one and two) on how to adjust my methods with the current user only so they have the specific user_id to edit,update, create and destroy their products.

Here is my code:

My Migration:

class CreateProducts < ActiveRecord::Migration
  def self.up
    create_table :products do |t|
      t.string :name
      t.date :date
      t.decimal  :price, :default => 0, :precision => 10, :scale => 2
      t.integer :user_id
      t.float :latitude
      t.float :longitude

the Controller:

class ProductsController < ApplicationController
  before_filter :authenticate_user!

  def index
    @products = Product.all

  def show
    @product = Product.find(params[:id])

  def new
   @product = Product.new

  def edit
    @product.user = current_user
    @product = Product.find(params[:id])
  end

  def create
    @product.user = current_user
    @product = Product.new(params[:product])

  def update
    @product.user = current_user
    @product = Product.find(params[:id])


  def destroy
    @product.user = current_user
    @product = Product.find(params[:id])
    @product.destroy

Product Model:

class Product < ActiveRecord::Base
    attr_accessible :name, :date, :price, :tag_list 
    belongs_to :user
end

then the Devise User Model:

class User < ActiveRecord::Base

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
  attr_accessible :email, :password, :password_confirmation, :remember_me 

  has_many :products,        :dependent => :destroy

I try to submit it and then suddenly this pops up:

NoMethodError in ProductsController#create

undefined method `user=' for nil:NilClass

What am i missing or doing wrong?

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T05:13:08+00:00Added an answer on May 23, 2026 at 5:13 am

    You are trying to assign @product.user before you have a product. Do your find on the product first, then assign the user.

    @product = Product.find(params[:id])
    @product.user = current_user
    

    For the actions other than create where you want to restrict the product to the current_user, you can do something like this (assuming you setup the has_many :products association in your User model.

    @product = current_user.products.find(params[:id])
    

    That will restrict find to just those products that have the user_id equal to the current_user.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I followed these instructions here: http://logback.qos.ch/consolePlugin.html I have the correct and found logback.xml, it
I followed these two exemples of developer.android (Creating an Input Method, Soft Keyboard sample).
I have followed these instructions exactly, yet I get a Page cannot be found
permissions don't seem to make any difference... In the manifest, I have only one
I followed these steps on https://github.com/karmi/tire to install elasticsearch for use with ruby on
I followed these instructions while trying to get remote debugging working with Delphi 2007.
I have an asp.net page which needs to return an object with a one-to-many
Hi a grep regex question I think I have many cases of two similar
We have a JMS queue of job statuses, and two identical processes pulling from
We have two websites for the same client (main www site and another for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.