我有三个模型,我试图使用简单的形式gem和复选框保存到第三个表。
class Work < ActiveRecord::Base
has_many :skills, through: :skillships
end
第二个模型是
class Skill < ActiveRecord::Base
has_many :works, through: :skillships
end
第三是
class Skillship < ActiveRecord::Base
belongs_to :work
belongs_to :skill
end
使用工作模型我试图将数据保存在技能表上。类似于http://railscasts.com/episodes/17-habtm-checkboxes-revised的东西。你能帮忙吗?
修改
表格
<%= simple_form_for(@work) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :title, :label => 'Project Title' %>
<%= f.input :excerpt, :as => :text %>
<fieldset>
<legend>Skills Used </legend>
Would like to check the skills i used here.
</fieldset>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
我试过..
<%= hidden_field_tag "work[skill_ids][]", nil %>
<% Skill.all.each do |skill| %>
<%= check_box_tag "work[skill_ids][]", skill.id, @work.skill_ids.include?(skill.id), id: dom_id(skill) %>
<%= label_tag dom_id(skill), skill.title %><br>
<% end %>
我这样做的原因是因为工作可以使用很多技能。
答案 0 :(得分:1)
rails g migration create_skills_works_table
然后
class CreateSkillsWorksTable < ActiveRecord::Migration
def self.up
create_table :skills_works, :id => false do |t|
t.references :skill
t.references :work
end
add_index :skills_works, [:skill_id, :work_id]
add_index :skills_works, [:work_id, :skill_id]
end
def self.down
drop_table :skills_works
end
end
在工作视图中使用简单表单。
<fieldset>
<legend>Skills Used </legend>
<%= f.association :skills %>
</fieldset>