【问题标题】:How to pre-populate collection_check_boxes in an edit form?如何在编辑表单中预填充 collection_check_boxes?
【发布时间】:2015-08-26 07:41:54
【问题描述】:

GitHub 仓库:https://github.com/Yorkshireman/mywordlist

我已经用谷歌搜索了这个。我确定有一种方法,可能需要在 html 选项哈希中添加一些代码,但我无法解决。有什么想法吗?

当访问具有一个或多个类别的 Word 的 _edit_word_form.html.erb 部分时,类别复选框都未选中,即使用户不想更改类别,也需要再次选择它们。

:title 和 :description 的文本字段已预先填充(谢天谢地)。

_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 %>

相关部分的话/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!

  # GET /words
  # GET /words.json
  def index
    @words = Word.all
    @quotes = Quote.all
  end

  # GET /words/1
  # GET /words/1.json
  def show
  end

  # GET /words/new
  def new
    @word = current_user.word_list.words.build
  end

  # GET /words/1/edit
  def edit
  end

  # POST /words
  # POST /words.json
  def create
    @word = Word.new(word_params)

    respond_to do |format|
      if @word.save
        format.html { redirect_to @word, notice: 'Word was successfully created.' }
        format.json { render :show, status: :created, location: @word }
      else
        format.html { render :new }
        format.json { render json: @word.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /words/1
  # PATCH/PUT /words/1.json
  def update
    #need to first remove categories from the word
    @word.categories.each do |category|
      @word.categories.delete category
    end

    #then push categories in from the category_params
    if params["category"].include?(:category_ids)
      (params["category"])["category_ids"].each do |i|
        next if i.to_i == 0
        @word.categories << Category.find(i.to_i) unless @word.categories.include?(Category.find(i.to_i))
      end
    end

    if category_params.include?(:title) && ((params["category"])["title"]) != ""
      @word.categories << current_user.word_list.categories.build(title: (params["category"])["title"])
    end

    respond_to do |format|
      if @word.update(word_params)
        format.html { redirect_to words_path, notice: 'Word was successfully updated.' }
        format.json { render :show, status: :ok, location: @word }
      else
        format.html { render :edit }
        format.json { render json: @word.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /words/1
  # DELETE /words/1.json
  def destroy
    @word.destroy
    respond_to do |format|
      format.html { redirect_to words_url, notice: 'Word was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_word
      @word = Word.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def word_params
      params.require(:word).permit(:title, :description, :category_ids)
    end

    def category_params
      params.require(:category).permit(:title, :category_ids, :category_id)
    end
end

categories_controller.rb:

class CategoriesController < ApplicationController
  before_action :set_category, only: [:show, :edit, :update, :destroy]

  # GET /categories
  # GET /categories.json
  def index
    @categories = Category.all
  end

  # GET /categories/1
  # GET /categories/1.json
  def show
  end

  # GET /categories/new
  def new
    @category = current_user.word_list.categories.build
  end

  # GET /categories/1/edit
  def edit
  end

  # POST /categories
  # POST /categories.json
  def create
    @category = Category.new(category_params)

    respond_to do |format|
      if @category.save
        format.html { redirect_to @category, notice: 'Category was successfully created.' }
        format.json { render :show, status: :created, location: @category }
      else
        format.html { render :new }
        format.json { render json: @category.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /categories/1
  # PATCH/PUT /categories/1.json
  def update
    respond_to do |format|
      if @category.update(category_params)
        format.html { redirect_to @category, notice: 'Category was successfully updated.' }
        format.json { render :show, status: :ok, location: @category }
      else
        format.html { render :edit }
        format.json { render json: @category.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /categories/1
  # DELETE /categories/1.json
  def destroy
    @category.destroy
    respond_to do |format|
      format.html { redirect_to categories_url, notice: 'Category was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_category
      @category = Category.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def category_params
      params.require(:category).permit(:title, :word_list_id, :category_ids)
    end
end

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

  # GET /word_lists
  # GET /word_lists.json
  def index
    @word_lists = WordList.all
  end

  # GET /word_lists/1
  # GET /word_lists/1.json
  def show
    @words = Word.all
    @word_list = WordList.find(params[:id])
  end

  # GET /word_lists/new
  def new
    @word_list = WordList.new
  end

  # GET /word_lists/1/edit
  def edit
  end

  # POST /word_lists
  # POST /word_lists.json
  def create
    @word_list = WordList.new(word_list_params)

    respond_to do |format|
      if @word_list.save
        format.html { redirect_to @word_list, notice: 'Word list was successfully created.' }
        format.json { render :show, status: :created, location: @word_list }
      else
        format.html { render :new }
        format.json { render json: @word_list.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /word_lists/1
  # PATCH/PUT /word_lists/1.json
  def update
    respond_to do |format|
      if @word_list.update(word_list_params)
        format.html { redirect_to @word_list, notice: 'Word list was successfully updated.' }
        format.json { render :show, status: :ok, location: @word_list }
      else
        format.html { render :edit }
        format.json { render json: @word_list.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /word_lists/1
  # DELETE /word_lists/1.json
  def destroy
    @word_list.destroy
    respond_to do |format|
      format.html { redirect_to word_lists_url, notice: 'Word list was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_word_list
      @word_list = WordList.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def word_list_params
      params[:word_list]
    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" }

  # The priority is based upon order of creation: first created -> highest priority.
  # See how all your routes lay out with "rake routes".

  # You can have the root of your site routed with "root"
  # root 'welcome#index'
  root 'pages#home'

  post 'create_word_and_category' => 'new_word#create_word_and_category'
end

【问题讨论】:

    标签: ruby-on-rails ruby forms ruby-on-rails-4 checkbox


    【解决方案1】:

    讨论可能不再活跃,但我会为未来的访客分享我的答案。

    添加 "{ checked: @array.map(&:to_param) }" 选项作为 collection_check_boxes 的最后一个参数可能会解决您的问题。请参考此link

    示例:
    假设您有软件模型和平台(OS)模型,并且想要选择一个或多个支持您的软件的操作系统。

    #views/softwares/edit.html.erb
    
    <%= form_for @software do |f| %>
      ...
      <%= f.label :supported_platform %>
      <%= f.collection_check_boxes(:platform_ids, @platforms, :id, :platform_name, { checked: @software.platform_ids.map(&:to_param) }) %>
      ...
      <%= f.submit "Save", class: 'btn btn-success' %>
    <% end %>
    

    注意:
    @software.platform_ids 应该是一个数组。如果您使用 SQLite,则在提取数据时必须将字符串转换为数组。我用 SQLite 对其进行了测试,并确认它也能正常工作。详情请参阅my post

    【讨论】:

      【解决方案2】:

      怎么了

      当您在fields_for :category 中调用category_form.collection_check_boxes(:category_ids, current_user.word_list.categories.all, :id, :title) 时,您是在说@word.category 上有一个名为category_ids 的方法将返回与@word.category 相关的类别的ID。如果在category_ids 结果和current_user.word_list.categories.all 中都有匹配的 id,则会选中该复选框。

      我认为没有@word.category@word.category.category_ids。我认为有@word.categories

      如何解决

      话虽如此,我的直觉告诉我你需要使用类似的东西:

      <%= form_for(@word) do |f| %>
          <div class="field form-group">
            <%= f.label(:title, "Word:") %><br>
            <%= f.text_field(:title, id: "new_word", required: true, autofocus: true, class: "form-control") %>
          </div>
      
          <div class="field form-group">
            <%= f.label(:description, "Definition:") %><br>
            <%= f.text_area(:description, class: "form-control") %>
          </div>
      
          <% if current_user.word_list.categories.count > 0 %>
            <div class="field form-group">
              <%= f.label(:categories, "Choose from existing Categories:") %><br>
              <%= f.collection_check_boxes(:category_ids, current_user.word_list.categories.all, :id, :title) %>
      
            </div>
          <% end %>
      
        <div class="actions">
          <%= f.submit("Update!", class: "btn btn-block btn-primary btn-lg") %>
        </div>
      <% end %>
      

      请注意,我已将collection_check_boxes 移至@word 的表单构建器,因为您希望为当前@word 构建“类别”值。我认为无论如何这应该是朝着正确方向迈出的一步。

      【讨论】:

      • 我得到一个“:title:Symbol 的未定义方法‘合并’”;似乎来自第 15 行
      • 糟糕,我认为现在应该修复它。对不起。
      • 不,一样。第 15 行是标签行之后的行 :-(
      • PS 供您参考,在控制台中,Word.first.categories 有效,Word.first.category_ids 返回类别 ID 数组,Word.first.categories[1].id 返回 48 (类别 ID)。我不知道为什么你的解决方案也不起作用
      • 不知道是否有帮助,但我已将 word_lists 控制器和 word_list 模型添加到问题中
      【解决方案3】:

      所以用户正在从他们的单词列表中编辑一个单词,但是您希望为他们的单词列表中的所有单词显示所有类别的复选框,检查附加到正在编辑的单词的那些类别。对吗?

      您似乎遗漏了 #collection_check_boxes 中的第一个参数,它应该是您调用 :category_ids 的对象。

      如果对象是用户的 word_list,那么类似:

      <%= category_form.collection_check_boxes(current_user.word_list, :category_ids, current_user.word_list.categories.all, :id, :title) do |b| %>
      

      可能不是确切的答案——我无法测试它——但希望它能给你一些帮助。

      【讨论】:

      • 谢谢,但没用 :-( Got a NoMethodError "undefined method `merge' for :title:Symbol"
      【解决方案4】:

      尝试像这样更改您的_edit_word_form.html.erb

      <%= form_for(@word) do |f| %>
      
          <div class="field form-group">
            <%= f.label(:title, "Word:") %><br>
            <%= f.text_field(:title, id: "new_word", required: true, autofocus: true, class: "form-control") %>
          </div>
      
          <div class="field form-group">
            <%= f.label(:description, "Definition:") %><br>
            <%= f.text_area(:description, class: "form-control") %>
          </div>
      
        <%= f.fields_for :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">
          <%= f.submit("Update!", class: "btn btn-block btn-primary btn-lg") %>
        </div>
      <% end %>
      

      【讨论】:

      • 恐怕没有变化:-(
      【解决方案5】:

      这应该更接近您要查找的内容:

      <%= b.label(class: "checkbox-inline", :"data-value" => b.value) { b.check_box + b.text } %>
      

      【讨论】:

      • 这个应该工作,据我所知,根据本页底部:apidock.com/rails/v4.0.2/ActionView/Helpers/FormOptionsHelper/… 但它没有:-( 这不会抛出解决任何错误,但也没有任何明显的区别。
      • &lt;%= b.label(class: "checkbox-inline") { b.check_box(:"data-value" =&gt; b.value) + b.text } %&gt;
      • 对不起,也没有任何区别 :-(
      猜你喜欢
      • 2016-02-05
      • 2022-10-08
      • 2016-06-23
      • 2022-10-15
      • 2011-11-15
      • 1970-01-01
      • 1970-01-01
      • 2019-12-10
      • 2013-01-09
      相关资源
      最近更新 更多