ga-wdi-exercises / project2

[project]
1 stars 30 forks source link

form to add multiple tags to a post #895

Closed colleeno closed 7 years ago

colleeno commented 7 years ago

I have a "categories" table, and "scripts" table (collection of tv/movie quotes), joined by tags.

Question 1 - I can only find examples (like our scribble exercise) to add tags(categories) after the item is created. Is there not a way to select when creating?

Question 2 - Either way, I've tested 3 ways to add tags on the "script" (quotes) edit page, having troubles setting them. Ideally I'd show preset tags, and a user could check multiples.

The checkbox works - this is my ideal, but it only creates 1 tag at a time. Is there a way to allow multiple selections to trigger multiple tags?

Text field works, but I don't want to use since I want to only allow preset tags. Skip this one.

Dropdown was my backup option to only allow certain tags, I'm displaying the options but not setting them. If I can't get multiples from a checkbox, curious how to use this to set.

form options and tag controller below, repo: repo: https://github.com/colleeno/project-scripts


<%= form_for [@script, @tag] do |f| %>
  <%= check_box_tag(:category_name, "TV") %>
  <%= label_tag(:category_name, "TV") %>
  <%= check_box_tag(:category_name, "Film") %>
  <%= label_tag(:category_name, "Film") %>
  <%= check_box_tag(:category_name, "Life") %>
  <%= label_tag(:category_name, "Life") %>
  <%= check_box_tag(:category_name, "Humor") %>
  <%= label_tag(:category_name, "Humor") %>
  <%= f.submit "add tag" %>
<% end %>

<%= form_for [@script, @tag] do |f| %>
  <%= text_field_tag :category_name %>
  <%= f.submit "add tag" %>
<% end %>

<%= form_for [@script, @tag] do |f| %>
<%= f.collection_select(:category_id, Category.all, :id, :name ) %>
<%= f.submit "add tag" %>
<% end %>

class TagsController < ApplicationController
  def new
    @script = Script.find(params[:script_id])
    @tag = @script.tags.new
  end

  def create
    @script = Script.find(params[:script_id])
    @category = Category.find_by(name: params[:category_name])
    existing_tag = Tag.find_by(script: @script, category: @category)
    unless existing_tag
      @script.tags.create(category: @category)
    end
    redirect_to script_path(@script)
  end

end
AndyWhitley commented 7 years ago

Hi Colleen, creating multiple models via the same controller is a bit tricky. What you can do is create checkboxes on the new view for each category, then in the create controller, iterate through the parameters and create an instance of your join model for each marked checkbox. For example:

A new view for creating products with category associations: https://github.com/AWhitleyGA/agora/blob/master/app/views/products/new.html.erb#L20-L25

Ignore the user parts of the form, they were specific to my domain

The associated create action: https://github.com/AWhitleyGA/agora/blob/master/app/controllers/products_controller.rb#L18-L32

Similar functionality on the edit view: https://github.com/AWhitleyGA/agora/blob/master/app/views/products/edit.html.erb#L20-L29

Associated update action: https://github.com/AWhitleyGA/agora/blob/master/app/controllers/products_controller.rb#L38-L52

AndyWhitley commented 7 years ago

A more complex but better practice for handling this is via nested forms. Give this section a read and see if you can adapt it to your domain:

http://guides.rubyonrails.org/form_helpers.html#building-complex-forms

colleeno commented 7 years ago

ahhh thanks! ill check this all out!