jeudi 8 février 2018

Unpermitted parameter: :companies

class CreateJobs < ActiveRecord::Migration[5.1]
  def change
    create_table :jobs do |t|
      t.string :title
      t.text :description
      t.string :c_name
      t.integer :user_id
      t.integer :company_id
      t.timestamps
    end
  end
end

class CreateCompanies < ActiveRecord::Migration[5.1]
  def change
    create_table :companies do |t|
      t.string :c_name
      t.text :c_description
      t.integer:user_id
      t.timestamps
    end
  end
end

# Models
class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  has_many :companies
  has_many :jobs
end

class Job < ApplicationRecord
  belongs_to :user
  belongs_to :category
  belongs_to :company
end

class Company < ApplicationRecord
  belongs_to:user
  has_many:jobs

end

# Jobs controller
def show
  end

  def new

    @job = current_user.jobs.build       
  end

  def create

        job_attrs = jobs_params.except(:company_id)
        company = Company.find_by_user_id(current_user.id)
        job_attrs = Company.find_by(user_id: jobs_params[:company_id])
       if job_attrs
    @job = current_user.jobs.build(job_attrs)

    if @job.save
      flash[:success]= "success"
      redirect_to @job

      else

      flash[:error]=@job.errors.full_messages
      render "new"
      end
    end

    def jobs_params
      params.require(:job).permit(:title, :description, :c_name, :category_id, :image,:jobclosedate,:company_id)
    end`enter code here`

when this happened company user log the system and try to create a job,

Processing by JobsController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"PE5K1+5jBCbS8DBAmF1uQBii3QmuAqBJ0Wg89mwO9Y/jMCbTVON8yhpEBL88XLyJRhJr3aZ/ZLDOnDrv0bcdng==", "job"=>{"title"=>"job job", "description"=>"hhhhhh jjjjjjjjjj", "companies"=>{"c_name"=>""}, "category_id"=>"1", "jobclosedate"=>""}, "commit"=>"Create Job"}

Unpermitted parameter: :companies [1m[36mCompany Load (0.5ms)[0m [1m[34mSELECT "companies".* FROM "companies" WHERE "companies"."user_id" = ? LIMIT ?[0m [["user_id", 1], ["LIMIT", 1]] Unpermitted parameter: :companies [1m[36mCompany Load (3.5ms)[0m [1m[34mSELECT "companies".* FROM "companies" WHERE "companies"."user_id" IS NULL LIMIT ?[0m [["LIMIT", 1]] No template found for JobsController#create, rendering head :no_content Completed 204 No Content in 1839ms (ActiveRecord: 9.0ms)"

Aucun commentaire:

Enregistrer un commentaire