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

扶醉桌前 提交于 2020-01-15 03:54:07

问题


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.


回答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




回答2:


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



来源:https://stackoverflow.com/questions/41627957/rails-5-acts-as-taggable-on-gem-simple-form-collection-select-for-defined-ta

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!