mercredi 6 avril 2016

Cannot save check_box_tag data with has_many through

I am trying to save checkbox data in project table

In Project Controller

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

In Project new view

Tags
<% @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 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

On my logs I get tag_ids parameter as above

project"=>{"tag_ids"=>["1", "2"]}

Q. Do I have to save data to Tagging table separately or does it automatically saves data because of the associations in the table?




Aucun commentaire:

Enregistrer un commentaire