Search code examples
ruby-on-railsruby-on-rails-5associationshas-manybelongs-to

Having issues creating and saving an instance of a nested resource


I am running into issues creating and saving an instance of a nested resource and I haven't been able to figure how to remedy this. I have a User and a Jobs model (User has_many jobs and Job belongs_to User). Existing users should be able to post/create jobs through there account.

In console @user.jobs.build / @user.jobs.create execute fine. In views, however, when I click "Post a Job" and attempt to submit the corresponding form, I receive the below errors (depending on what I change):

  1. When I submit the form to jobs#new with @user = User.find(params[:id]) and @job = @user.jobs.build(user_jobs_params), the error is:

    ActionController::ParameterMissing (param is missing or the value is empty:

  2. When I submit the form to jobs#create with @user = User.find(params[:id]) and @job = @user.jobs.build(user_jobs_params), the error is:

    ActiveRecord::RecordNotFound (Couldn't find User with 'id'=):

    SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", nil], ["LIMIT", 1]]

  3. When I submit the form to jobs#create with @user = current_user and @job = @user.jobs.build(user_jobs_params), the error is:

    ActionController::ParameterMissing (param is missing or the value is empty:

    SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", nil], ["LIMIT", 1]]

  4. When I submit the form to jobs#create with @user = current_user and @job = @user.jobs.build(user_jobs_params) set my form up with <%= form_with(model: @user, :url => { :controller => "job", :action => "create" }, :html => {:method => :post}) do |f| %>, the error is:

    ActionController::ParameterMissing (param is missing or the value is empty:

    SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", nil], ["LIMIT", 1]]

I been wrecking my brains trying to understand why user_id is nil/unable to be found when in the server log, the user is loaded on every page of the website. Especially confused for the cases using current_user as that's defined in my sessions helper and needs to exist for a user to log in). I also tried doing this with @user = User.new and ran into some other errors that I can add if that's the appropriate way to do this, however, that didn't seem to make sense since I'm not trying to create a new user with the jobs posts but rather create a job that belongs_to an existing user.

UPDATING: Jobs Controller

def new
    @user = User.find(params[:user_id])
    @job = @user.jobss.build
  end

Tried pushing create two different ways and neither worked!

  def create
    @user = User.find(params[:user_id])
    @job = @user.jobs.build(user_job_params)
    @job.save
  end

  def create
    @user = User.find(params[:user_id])
    @job = @user.jobs.create(user_job_params)
  end

  private

  def user_job_params
    params.require(:job).permit(:job_name, 
                              :job_description, :job_deadline, :organization,
                              :organization_liaison_name, :organization_liaison_email)
  end
end

Top of User Controller

class User < ApplicationRecord
  has_one :partner_profile
  has_one :student_profile
  has_many :jobs, inverse_of: :user


  accepts_nested_attributes_for :student_profile, :partner_profile, update_only: true
  attr_accessor :remember_token, :activation_token, :reset_token
  before_save   :downcase_email
  before_create :create_activation_digest, :build_profile
  validates :first_name,  presence: true, length: { maximum: 50 }
  validates :last_name,  presence: true, length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
  validates :email, presence: true, length: { maximum: 255 },
                      format: { with: VALID_EMAIL_REGEX },
                      uniqueness: { case_sensitive: false }
  has_secure_password
  validates :password, presence: true, length: { minimum: 6 }, allow_nil: true
  validates :account_type, presence: true

I am running into issues creating and saving an instance of a nested resource and I haven't been able to figure how to remedy this. I have a User and a Jobs model (User has_many jobs and Job belongs_to User). Existing users should be able to post/create jobs through there account.

In console @user.jobs.build / @user.jobs.create execute fine. In views, however, when I click "Post a Job" and attempt to submit the corresponding form, I receive the below errors (depending on what I change):

  1. When I submit the form to jobs#new with @user = User.find(params[:id]) and @job = @user.jobs.build(user_jobs_params), the error is:

    ActionController::ParameterMissing (param is missing or the value is empty:

  2. When I submit the form to jobs#create with @user = User.find(params[:id]) and @job = @user.jobs.build(user_jobs_params), the error is:

    ActiveRecord::RecordNotFound (Couldn't find User with 'id'=):

    SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", nil], ["LIMIT", 1]]

  3. When I submit the form to jobs#create with @user = current_user and @job = @user.jobs.build(user_jobs_params), the error is:

    ActionController::ParameterMissing (param is missing or the value is empty:

    SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", nil], ["LIMIT", 1]]

  4. When I submit the form to jobs#create with @user = current_user and @job = @user.jobs.build(user_jobs_params) set my form up with <%= form_with(model: @user, :url => { :controller => "job", :action => "create" }, :html => {:method => :post}) do |f| %>, the error is:

    ActionController::ParameterMissing (param is missing or the value is empty:

    SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", nil], ["LIMIT", 1]]

I been wrecking my brains trying to understand why user_id is nil/unable to be found when in the server log, the user is loaded on every page of the website. Especially confused for the cases using current_user as that's defined in my sessions helper and needs to exist for a user to log in). I also tried doing this with @user = User.new and ran into some other errors that I can add if that's the appropriate way to do this, however, that didn't seem to make sense since I'm not trying to create a new user with the jobs posts but rather create a job that belongs_to an existing user.

UPDATING: Jobs Controller

def new
    @user = User.find(params[:user_id])
    @job = @user.jobss.build
  end

  def create
    @user = User.find(params[:user_id])
    @job = @user.jobs.build(user_job_params)
    @job.save
  end

  def create
    @user = User.find(params[:user_id])
    @job = @user.jobs.create(user_job_params)
  end

  private

  def user_job_params
    params.require(:job).permit(:job_name, 
                              :job_description, :job_deadline, :organization,
                              :organization_liaison_name, :organization_liaison_email)
  end
end

Top of User Controller

class User < ApplicationRecord
  has_one :partner_profile
  has_one :student_profile
  has_many :jobs, inverse_of: :user


  accepts_nested_attributes_for :student_profile, :partner_profile, update_only: true
  attr_accessor :remember_token, :activation_token, :reset_token
  before_save   :downcase_email
  before_create :create_activation_digest, :build_profile
  validates :first_name,  presence: true, length: { maximum: 50 }
  validates :last_name,  presence: true, length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
  validates :email, presence: true, length: { maximum: 255 },
                      format: { with: VALID_EMAIL_REGEX },
                      uniqueness: { case_sensitive: false }
  has_secure_password
  validates :password, presence: true, length: { minimum: 6 }, allow_nil: true
  validates :account_type, presence: true

Job Creation ERROR

Started POST "/users/2/jobs" for XXXX at 2018-07-05 19:42:33 +0000
Cannot render console from XXXX! Allowed networks: XXXX
Processing by JobsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"I1/okrk5p125FKrFx2Nhu4L78UNJFQIZPgjWfnzVnCCh5vzr9AuNxFkl2jHD+qjshlh170OI1o0Av8yGlek4Aw==", "job"=>{"job_name"=>"ega", "job_description"=>"", job_deadline(2i)"=>"", "job_deadline(3i)"=>"", "job_deadline(1i)"=>"", "organization"=>"", "organization_liaison_name"=>"", "organization_liaison_email"=>"", "program_manager_first_name"=>"", "program_manager_last_name"=>"", "program_manager_email"=>"", "program_manager_phone_number"=>""}, "commit"=>"Submit A New Job!", "user_id"=>"2"}
  User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?  [["id", 2], ["LIMIT", 1]]
   (0.1ms)  begin transaction
  SQL (2.9ms)  INSERT INTO "jobs" ("user_id", job_name", "status", "job_description", "organization", "organization_liaison_name", "organization_liaison_email", "program_manager_first_name", "program_manager_last_name", "program_manager_email", "program_manager_phone_number", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)  [["user_id", 2], ["job_name", "ega"], ["status", "Pending Approval"], ["job_description", ""], ["organization", ""], ["organization_liaison_name", ""], ["organization_liaison_email", ""], ["program_manager_first_name", ""], ["program_manager_last_name", ""], ["program_manager_email", ""], ["program_manager_phone_number", ""], ["created_at", "2018-07-05 19:42:33.406656"], ["updated_at", "2018-07-05 19:42:33.406656"]]
   (5010.0ms)  commit transaction
   (2.3ms)  rollback transaction
Completed 500 Internal Server Error in 5023ms (ActiveRecord: 5015.4ms)



ActiveRecord::StatementInvalid (SQLite3::BusyException: database is locked: commit transaction):

app/controllers/jobs_controller.rb:28:in `create'

Solution

  • Since you are in the JobsController and not the UsersController, you need to grab the user's id with user_id from the params (not id). Try using...

    @user = User.find(params[:user_id])
    

    Also, you should not be passing in the params in the new action...only pass them in during the create action.

    In the new action

    @job = @user.jobs.build
    

    In the create action

    @job = @user.jobs.build(user_jobs_params)