Rails 5 - Acts as Taggable On - how do I add the tag from a form selection

447 views Asked by At

My Proposal.rb has:

class Proposal < ApplicationRecord include Statesman::Adapters::ActiveRecordQueries

  acts_as_taggable
  acts_as_taggable_on :randd_maturities, :randd_fields, :randd_purposes, :randd_activities

My proposal form.html.erb has:

<%= f.collection_select :randd_field_list, Randd::Field.order(:title), :id, :title, {}, {multiple: true} %>

My proposal controller whitelists the randd_field_list with:

  def proposal_params
      params.require(:proposal).permit(:title,  randd_maturities_list: [], randd_field_list: [], randd_purposes_list: [],   randd_activities_list: [],

When I try selecting a tag from my form, I get no errors but the tag doesnt save to the proposal:

Processing by ProposalsController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"MO4g02iQgHALwVGBgVp7UN9MQvRa5uaJ56e7/tV3TTqk2UW40CLfEdrJCgafFsBNSuZEoEApXHRl3xp0zmo7Iw==", "proposal"=>{"title"=>"asdf",  "randd_field_list"=>["", "1"]

How do I get the form selection title to save as the tag for that proposal?

1

There are 1 answers

1
Neeraj Rana On

Try using this code.

Add only this line in your model.

 acts_as_taggable_on :randd_maturities, :randd_fields, :randd_purposes, :randd_activities

Add this to your controller

    params.require(:proposal).permit(:title,  :randd_maturities_list, 
:randd_field_list, :randd_purposes_list,   :randd_activities_list)

Add this to your form. My code is for simple_form_for

 <%= f.input :randd_maturities_list %>

If you are using form for use this.

<%= f.text_field :randd_maturities_list %>