Rails 5 - Acts as Taggable On gem - simple form collection select for defined tag lists
Asked Answered
F

2

2

I am trying to learn how to use the Acts as Taggable On gem with Rails 5.

I use simple form for forms. I think part of the problem arises out of there not being an association on the models between proposal and randd_fields.

I have models called Proposal and Randd::Field. I am trying to tag proposals with tags which are the :title attribute of the Randd::Field table.

My models have:

Proposal

class Proposal < ApplicationRecord

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


# acts_as_taggable
# acts_as_taggable_on :skills, :interests

Randd::Field

(no association on Proposal).

Proposal helper

module ProposalsHelper

 include ActsAsTaggableOn::TagsHelper

In my proposal form, I try to add tags:

<%#= f.text_field :randd_field_list, input_type: "textbox", name:"proposal[randd_field_list][]", html_options: { style: 'width: 100%' } %>


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

The first commented form input field works to allow me to input a tag. However, the tags are text and I can write anything I want. This is not what I want. I want the choice of tags to be the :title attribute defined in my Randd::Field model.

The second option that I tried below does produce a list, but when I try to save it as the randd_field_list on the proposal model, it saves the :id attribute from the Randd::Field table instead of the :title.

When I try deleting the :id component from the form input line, I get an error that says:

{} is not a symbol nor a string

When I then try deleting the {}, from the form input line, I get an error saying:

{multiple: true} is not a symbol nor a string

Can anyone see how to use Acts as Taggable On gem so that the tags are predefined title attributes from the model that serves as the tagging object? I don't want freeform tags.

Proposal controller has:

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

Processing by ProposalsController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"Cy

then lower down in that block, this is the selection I made in the form:

"randd_field_list"=>"Aerospace Structures",

Then at the end of that block:

Unpermitted parameters: randd_maturity_list, randd_field_list
   (0.1ms)  BEGIN

I have tried each of these - none of them work.

 <%#= f.text_field :randd_field_list, input_type: "textbox", name:"proposal[randd_field_list][]", html_options: { style: 'width: 100%' } %>
        <%#= f.collection_select :randd_field_list, Randd::Field.order(:title),  :title, :title, input_html: { multiple: true } %>
        <%#= f.input 'randd_field_list',
       options_from_collection_for_select(@randd_fields, :title, :title),
       multiple: true %>
       <%#= f.collection_select 'randd_field_list',
      options_from_collection_for_select(@randd_fields, :title, :title),
      multiple: true %>
       <%= f.input :randd_field_list,:collection => @randd_fields,:label_method => :title,:value_method => :title,:label => "Fields" ,:include_blank => false, :multiple => true %>

The top one works to assign values to the randd_field_list, but it won't allow use of my pre-defined :title attributes on the Randd::Field table to be used as tags.

The rest of the attempts give an unpermitted param error when I try to save the form.

Filia answered 13/1, 2017 at 5:33 Comment(0)
T
1

Replace :id with :title.

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

You want to pass tag.title instead of id as the selected option value. http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/collection_select

Thumbscrew answered 13/1, 2017 at 5:43 Comment(16)
Hi Zhong, Thanks! This brings me back around to this error: Unpermitted parameters: randd_maturity_list, randd_field_list. I cant figure out how to make sense of the API dock documents. None of it makes sense to me. The form does render and the titles do make a list the way you suggested, but I cant save the selection and multiple: true doesnt work either.Filia
you need to add randd_maturity_list: [], randd_field_list: [] to your permitted_paramsThumbscrew
It is like that in my permitted params in the proposal controller. The error is unpermitted param when I try to use the form with the select field the way it is now. I don't get that error if I make the form field a free text option (but then I cant use my own defined tags)Filia
can you edit your question with code snippits of permitted_params method and request log when you are using select options?Thumbscrew
Hi - I added them to the end of the postFilia
thanks. can you edit with the request log that you selected multiple tags? I suspect it is passing as 'randd_maturity_list' = >'a, b, c'. If this is the case, use :randd_maturity_list, :randd_field_list for permitted_params instead.Thumbscrew
I cant force it to select multiple tags. When I use cmd click (on a mac) - it just selects the second option and forgets the first (just the same if I didnt use cmd click and just clicked.Filia
Although - i currently have 2 tags assigned by using the rails console - so there must be a way for it to accept an array -just not via the form (using the setup i currently have)Filia
can you try this syntax {:multiple => true}?Thumbscrew
I can still only choose oneFilia
The 2nd answer on this post suggested another syntax, but that doesn't work either #22160114Filia
I also tried this formulation but it gives an error that makes no sense at all: <%= f.select_tag 'randd_field_list', options_from_collection_for_select(@randd_fields, :title, :title), multiple: true %>Filia
The error says: undefined method `map' for nil:NilClass Did you mean? tap; but I haven't written map anywhereFilia
I suppose you can have a look of select2, which support multiple selection as dropdown.Thumbscrew
do you think its more about simple form than it is about acts as taggable on gem? Simple form has a multiple: true option which you can give to the select form. Why would it do that if it cant be used?Filia
I cant get this to work (just a flag in case someone else is stuck on this problem too) - but giving points because you've helped me so much with other thingsFilia
U
1

To solve

Replace :id with :title

Like what Zhong said.

And the problem you are having I think is because your code is singular. Instead it should be plural. Hopes it helps

Ulotrichous answered 26/1, 2017 at 4:16 Comment(1)
Hi Yahs - I set out the issue that I'm encountering with Zhong's suggestion in the comments beneath his answer. Which bit of my singular code do you think is causing the problem?Filia

© 2022 - 2024 — McMap. All rights reserved.