GitHub repo: https://github.com/Yorkshireman/mywordlist
I got out of this trait. I'm sure there is a way, possibly requiring some kind of code inside the html hash parameters, but I can't handle it. Any ideas?
When you visit the _edit_word_form.html.erb partial for Word that has one or more categories, the Categories checkboxes are all unchecked, requiring the user to select them again, even if they do not want to change the categories.
Text fields for: title and: description are PRELIMINARY filled (fortunately).
_edit_word_form.html.erb:
<%= form_for(@word) do %> <%= fields_for :word, @word do |word_form| %> <div class="field form-group"> <%= word_form.label(:title, "Word:") %><br> <%= word_form.text_field(:title, id: "new_word", required: true, autofocus: true, class: "form-control") %> </div> <div class="field form-group"> <%= word_form.label(:description, "Definition:") %><br> <%= word_form.text_area(:description, class: "form-control") %> </div> <% end %> <%= fields_for :category, @category do |category_form| %> <% if current_user.word_list.categories.count > 0 %> <div class="field form-group"> <%= category_form.label(:title, "Choose from existing Categories:") %><br> <%= category_form.collection_check_boxes(:category_ids, current_user.word_list.categories.all, :id, :title) do |b| %> <%= b.label(class: "checkbox-inline") { b.check_box + b.text } %> <% end %> </div> <% end %> <h4>AND/OR...</h4> <div class="field form-group"> <%= category_form.label(:title, "Create and Use a New Category:") %><br> <%= category_form.text_field(:title, class: "form-control") %> </div> <% end %> <div class="actions"> <%= submit_tag("Update!", class: "btn btn-block btn-primary btn-lg") %> </div> <% end %>
Relevant part of the words /index.html.erb:
<% current_user.word_list.words.alphabetical_order_asc.each do |word| %> <tr> <td> <%= link_to edit_word_path(word) do %> <%= word.title %> <span class="glyphicon glyphicon-pencil"></span> <% end %> </td> <td><%= word.description %></td> <td> <% word.categories.alphabetical_order_asc.each do |category| %> <a class="btn btn-info btn-sm", role="button"> <%= category.title %> </a> <% end %> </td> <td> <%= link_to word, method: :delete, data: { confirm: 'Are you sure?' } do %> <span class="glyphicon glyphicon-remove"></span> <% end %> </td> </tr> <% end %>
words_controller.rb:
class WordsController < ApplicationController before_action :set_word, only: [:show, :edit, :update, :destroy] before_action :authenticate_user!
categories_controller.rb:
class CategoriesController < ApplicationController before_action :set_category, only: [:show, :edit, :update, :destroy]
word_lists_controller.rb:
class WordListsController < ApplicationController before_action :set_word_list, only: [:show, :edit, :update, :destroy] def from_category @selected = current_user.word_list.words.joins(:categories).where( categories: {id: (params[:category_id])} ) respond_to do |format| format.js end end def all_words respond_to do |format| format.js end end
word_list.rb:
class WordList < ActiveRecord::Base belongs_to :user has_many :words has_many :categories end
word.rb:
class Word < ActiveRecord::Base belongs_to :word_list has_and_belongs_to_many :categories validates :title, presence: true scope :alphabetical_order_asc, -> { order("title ASC") } end
category.rb:
class Category < ActiveRecord::Base has_and_belongs_to_many :words belongs_to :word_list validates :title, presence: true scope :alphabetical_order_asc, -> { order("title ASC") } end
schema.rb:
ActiveRecord::Schema.define(version: 20150609234013) do create_table "categories", force: :cascade do |t| t.string "title" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.integer "word_list_id" end add_index "categories", ["word_list_id"], name: "index_categories_on_word_list_id" create_table "categories_words", id: false, force: :cascade do |t| t.integer "category_id" t.integer "word_id" end add_index "categories_words", ["category_id"], name: "index_categories_words_on_category_id" add_index "categories_words", ["word_id"], name: "index_categories_words_on_word_id" create_table "quotes", force: :cascade do |t| t.text "content" t.string "author" t.datetime "created_at", null: false t.datetime "updated_at", null: false end create_table "users", force: :cascade do |t| t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: false t.string "reset_password_token" t.datetime "reset_password_sent_at" t.datetime "remember_created_at" t.integer "sign_in_count", default: 0, null: false t.datetime "current_sign_in_at" t.datetime "last_sign_in_at" t.string "current_sign_in_ip" t.string "last_sign_in_ip" t.datetime "created_at", null: false t.datetime "updated_at", null: false end add_index "users", ["email"], name: "index_users_on_email", unique: true add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true create_table "word_lists", force: :cascade do |t| t.datetime "created_at", null: false t.datetime "updated_at", null: false t.integer "user_id" end create_table "words", force: :cascade do |t| t.string "title" t.text "description" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.integer "word_list_id" end add_index "words", ["word_list_id"], name: "index_words_on_word_list_id" end
routes.rb:
Rails.application.routes.draw do resources :quotes resources :categories resources :words devise_for :users, controllers: { registrations: "users/registrations" }