Reputation: 1623
I am using the acts-as-taggable-on
gem as well as the select2.js
library.
My community_resource.rb
looks like the following:
class CommunityResource < ApplicationRecord
acts_as_taggable_on :recovery_tags
end
My views/community_resources/_form.html.erb
view looks like the following:
In my views/community_resources/_form.html.erb
file my (1) traditional form.textfield is written as follows:
<div class='form-group'>
<%= form.label :recovery_tag_list %>
<%= form.text_field :recovery_tag_list, value: @community_resource.send(:recovery_tag_list).to_s %>
</div>
My (2) Select2.js form field (that does not currently save on submit that I am trying to get to work) is written as follows:
<select class="form-control js-example-basic-multiple" multiple="multiple">
<%# Loop through all recovery_tag values %>
<% for tag in ActsAsTaggableOn::Tag.for_context(:recovery_tags) %>
<%# If the the tag is listed in the current @community_resource, output as a "selected" option %>
<% if @community_resource.send(:recovery_tag_list).include? tag.name %>
<option selected="selected"><%= tag.name %></option>
<%# Else output tag's name as an unselected option %>
<% else %>
<option><%= tag.name %></option>
<% end %>
<% end %>
</select>
If helpful here is the whole view: Git gist of views/community_resources/_form.html.erb
I am initializing the Select2 selection element with (which works great)
document.addEventListener("DOMContentLoaded", function(event) {
$(document).ready(function() {
$('.js-example-basic-multiple').select2({ });
});
});
My CommunityResource controller if helpful:
class CommunityResourcesController < ApplicationController
before_action :set_community_resource, only: %i[ show edit update destroy ]
# GET /community_resources or /community_resources.json
def index
@community_resources = CommunityResource.all
end
# GET /community_resources/1 or /community_resources/1.json
def show
end
# GET /community_resources/new
def new
@community_resource = CommunityResource.new
end
# GET /community_resources/1/edit
def edit
end
# POST /community_resources or /community_resources.json
def create
@community_resource = CommunityResource.new(community_resource_params)
respond_to do |format|
if @community_resource.save
format.html { redirect_to @community_resource, notice: "Community resource was successfully created." }
format.json { render :show, status: :created, location: @community_resource }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @community_resource.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /community_resources/1 or /community_resources/1.json
def update
respond_to do |format|
if @community_resource.update(community_resource_params)
format.html { redirect_to @community_resource, notice: "Community resource was successfully updated." }
format.json { render :show, status: :ok, location: @community_resource }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @community_resource.errors, status: :unprocessable_entity }
end
end
end
# DELETE /community_resources/1 or /community_resources/1.json
def destroy
@community_resource.destroy!
respond_to do |format|
format.html { redirect_to community_resources_path, status: :see_other, notice: "Community resource was successfully destroyed." }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_community_resource
@community_resource = CommunityResource.find(params[:id])
end
# Only allow a list of trusted parameters through.
def community_resource_params
params.require(:community_resource).permit(:name, :hours_of_operation, :phone, :address, :website, :information, :recovery_tag_list)
end
end
How can I write my Select2 element that presently only outputs my recovery resource tags, so that it saves selected options of recovery resource tags on submitting the form to :recovery_tags
? Maybe the real question I'm trying to ask is how can I use straight HTML instead of form helper.
Upvotes: 0
Views: 48