when i'm try to create a job, it will show company must exit, c_name and company_id will not pass in the views/jobs/_form
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(:c_name)
job_attrs[:c_name] = Company.find_by(id: jobs_params[:c_name])
@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
in the jobs/_form
<%= simple_form_for(@job,validation:true ,html: { mutlipart: true, class: 'form-horizontal'}) do |f| %>
<%= f.input :title, label: "Job Title", input_html: { class: "form-control"}%>
<%= f.input :description, label: "Job Description", input_html: { class: "form-control" }%>
<%= f.input :c_name, label: "Your Company", input_html: { class: "form-control" }%>
<%= f.collection_select :category_id,Category.all, :id, :name, {promt: "Choose a category" }%>
when i'm try to create a job, it will show company must exit, c_name and company_id will not pass in the views/jobs/_form . could you please help me to sort it out.it's seems create def has issue. i'm new to ruby please explain me to sort this out.
Aucun commentaire:
Enregistrer un commentaire