我正在尝试保存项目表中的复选框数据
在项目控制器中
def new
@project = Project.new
@allTags = Tag.all
@allBenefits = Benefit.all
end
def create
p params[:projectName]; # this always throws nil even there is a value
@project = Project.new(project_params)
#@tagging = @project.tagging.create!(:project=>Project.id,:tag=>1)
if @project.save
redirect_to :action => 'index'
else
render :action => 'new'
end
end
def project_params
params.require(:com_a_b_c_project).permit(:projectName, :briefDesc, :whatSection, :challengers, :status, :valueProposal, :maturityLevel,:tag_ids =>[])
end
在项目新视图中
<h4><label for = "tags">Tags</label></h4>
<% @allTags.each do |tag| %>
<p><%= f.label tag.tagName %></p>
<%= check_box_tag :tag_ids, tag.id, @project.tags.include?(tag), :name => 'project[tag_ids][]'-%>
<%end%>
class Com::a::b::c::Project < ActiveRecord::Base
has_many :taggings
has_many :tags, :through => :taggings
accepts_nested_attributes_for :tags
end
class Com::a::b::c::Tag < ActiveRecord::Base
has_many :taggings
has_many :projects, :through => :taggings
end
class Com::a::b::c::Tagging < ActiveRecord::Base
belongs_to :project
belongs_to :tag
end
在我的日志中,我得到了tag_ids参数,如下所示
project"=>{"tag_ids"=>["1", "2"]}
Q值。我是否必须单独将数据保存到标记表,还是由于表中的关联而自动保存数据?
目前没有任何内容保存在标记表上。如何存储标记数据?
答案 0 :(得分:0)
如果我按照正确的方式
class Com::a::b::c::Project < ActiveRecord::Base
has_many :taggings
has_many :tags, :through => :taggings
accepts_nested_attributes_for :tags
end
项目模型上的应允许持久保存。