2016-11-18 11 views
0

у меня есть модель работы, мастерство модель и job_skill моделькак сохранить данные в has_many_through таблицы при сохранении родительского объекта с nested_form рельсами

class Job < ActiveRecord::Base 
     has_many :job_skills 
     has_many :skills ,through: :job_skills 
     accepts_nested_attributes_for :job_skills 


class Skill < ActiveRecord::Base 
    has_many :job_skills 
    has_many :jobs ,through: :job_skills 
    accepts_nested_attributes_for :job_skills 


class JobSkill < ActiveRecord::Base 
    belongs_to :skill 
    belongs_to :job 
    accepts_nested_attributes_for :job 
    accepts_nested_attributes_for :skill 

и контроллер рабочих мест

class JobsController < ApplicationController 
    def new 
     @job = Job.new 
     @job.job_skills.build 
    end 

    def create 
     @job = Job.new(job_params) 
     @job.save 
    end 

    private 
    def job_params 
    params.require(:job).permit(:occupation, :industry, :location,job_skills_attributes:[]) 
    end 

и форма работы является

= form_for @job do |f| 
    = f.label :location 
    = f.text_field :location  
.clear 
    = f.label "industry*" 
    = f.text_field :industry 
.clear 
    = f.label :occupation 
    = f.text_field :occupation 
.clear 
    = f.label "Skill Required*" 
    = f.fields_for(:job_skills) do |s| 
    = s.select :skill_id, Skill.all.collect{|p| [p.skill, p.id]},{}, {:multiple => true, :class=> "chosen-select multiple-select-skills"} 
= f.submit "submit" 

только работа получить сохранить. jobs_skills не сохраняет. в параметрах работы я получаю только данные о заданиях. Что может быть причиной. пожалуйста помоги.!!

+0

При сохранении задания job_skill необходимо сохранить автоматически. Но это не так. – user3779329

+0

Вы должны использовать nested_form_for вместо form_for. – user100693

+0

nested_form_for - неопределенный метод. Значит, мне нужно использовать nested_form gem.right? – user3779329

ответ

0

Обновите form_for к nested_form_for

= nested_form_for @job do |f| 
    = f.label :location 
    = f.text_field :location  
.clear 
    = f.label "industry*" 
    = f.text_field :industry 
.clear 
    = f.label :occupation 
    = f.text_field :occupation 
.clear 
    = f.label "Skill Required*" 
    = f.fields_for(:job_skills) do |s| 
= s.select :skill_id, Skill.all.collect{|p| [p.skill, p.id]},{}, {:multiple => true, :class=> "chosen-select multiple-select-skills"} 
+0

nested_form_for - неопределенный метод. Значит, мне нужно использовать nested_form gem.right? – user3779329

+0

Да, вы правы. Для этого вам следует использовать жемчужину nested_form. – user100693

+0

все еще он не сохраняет навык ... только сохранение задания – user3779329

0

В этом случае accepts_nested_attributes_for не требуется.

Ваши модели должны быть

class Job < ActiveRecord::Base 
    has_many :job_skills 
    has_many :skills ,through: :job_skills 

class Skill < ActiveRecord::Base 
    has_many :job_skills 
    has_many :jobs ,through: :job_skills 

class JobSkill < ActiveRecord::Base 
    belongs_to :skill 
    belongs_to :job 

И работа контроллер

class JobsController < ApplicationController 
    def new 
    @job = Job.new 
    #@job.job_skills.build #=> No need of buid 
    end 

    def create 
    @job = Job.new(job_params) 
    @job.save 
    end 

    private 

    # Add skill_ids to job_params 
    def job_params 
    params.require(:job).permit(:occupation, :industry, :location, skill_ids:[]) 
    end 

и работа форма

= form_for @job do |f| 
    = f.label :location 
    = f.text_field :location  
    .clear 
    = f.label "industry*" 
    = f.text_field :industry 
    .clear 
    = f.label :occupation 
    = f.text_field :occupation 
    .clear 
    = f.label "Skill Required*" 
    = s.select :skill_ids, Skill.all.collect{|p| [p.skill, p.id]},{}, {:multiple => true, :class=> "chosen-select multiple-select-skills"} 
    = f.submit "submit" 

Надеюсь, это будет полезно