mysql - 用于编辑集合的 Rails 表单

标签 mysql ruby-on-rails ruby forms

我想要完成的是一个简单的“切换”复选框,用于打开/关闭包含模型记录的 View 。

我试图研究“隐藏”值,但它似乎没有按预期工作。

How to get blank checkboxes to pass as false to params

当我添加:<%= hidden_field_tag "category_ids[]", '' %>

我收到Couldn't find Category with 'category_name'=取消选中时。

本质上,该表是一个Model.all ,但我希望能够修改 is_active 上的键值列至truefalse ,取决于是否选中该框。目前,我可以通过“选中”该框来完成此操作。但是,不要取消选中(通过 null )。我正在尝试完成这一任务,这是一次飞跃,而不是进行所有检查,然后再进行一次未检查的检查。并且,还绕过显示/编辑过程。表的大小相当小,所以我不关心延迟。

我已尽我所能进行搜索,但不知所措。根据我的发现,我可以做其中之一,但不幸的是不能两者都做,我将非常感谢任何指导。

我的观点:

<h4>Categories</h4>
<%= form_tag enable_categories_path, method: :put do |f| %>
<table id="myTable" class="table table-bordered table-striped">
  <tr>
    <th>Enable</th>
    <th>Category Name</th>
    <th>Active</th>
  </tr>

  <tbody>
  <% @categories.each do |category| %>
    <tr>
      <td>
        <%= check_box_tag "category_ids[]", category.id, category.is_active == 1 ? 'checked' : nil %>
      </td>
      <td><%= link_to category.category_name, category %></td>
      <td><%= category.is_active == 1 ? 'Yes' : 'No' %></td>
    </tr>
  <% end %>
  </tbody>
</table>
<%= render 'settings/button' %>
<% end %>

在这里,复选框从模型本身获取相应记录的状态,因此如果不对复选框采取任何操作,它会保持不变(或将状态传回)

我的 Controller :

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

  def index
    @categories = Category.all.order(:category_sort)
  end

  def show
    @category = Category.find(params[:id])
  end

  def edit
  end

  def update
    if @category.update(category_params)
      redirect_to categories_path
    else
      render :edit
    end
  end

  def enable
    Category.update(params[:category_ids], params[:category_ids].map{ |e| {is_active: true} })
    redirect_to categories_path
  end

  private

  def set_category
    @category = Category.find(params[:id])
  end

  def category_params
    params[:category].permit(:id, :category_name, :is_active)
  end

end

目前,我只传递 is_active: true,直到我找到一种传递所有复选框状态的方法。

我的模型:

class Category < ActiveRecord::Base
  self.table_name  = 'categories'

  has_many   :metrics
end

我的路线:

resources :categories do
    collection do
      put :toggle
    end
  end

对于复选框来说,一切似乎都正确通过。但是,当某些内容未选中时,日志中不会显示任何内容。

最佳答案

当我在 Rails 中遇到这样的情况时,我通常最终会使用 AJAX 而不是批量分配。这实际上相当容易。至少对我来说比学习 check_boxes 的内部运作更容易和collection_check_boxes ,哈哈

一个简单的类别表:

    <table>
  <thead>
    <tr>
      <th>Category</th>
      <th>Status</th>
    </tr>
  </thead>

  <tbody>
    <% @categories.each do |category| %>
      <tr>
        <td><%= category.category_name %></td>
        <% status = category.is_active? ? 'Active' : 'Inactive' %>
        <td id="<%= category.id %>" ><button class="button_<%= status %>"><%= link_to status, toggle_active_path(:id => category.id), :method => :post, :remote => true %></button></td>
      </tr>
    <% end %>
  </tbody>
</table>

需要注意的行是嵌入的 ruby​​ 设置 status多变的。这用于设置按钮的类值。在 CSS 文件中,按钮类 button_Active将颜色设置为绿色和 button_Inactive使其变红。它还使用此变量来设置按钮的文本。

enter image description here

在 Controller 中创建一个切换状态的方法:

def toggle_is_active
@category.toggle!(:is_active) #flips the boolean on is_active
respond_to do |format|
  format.html { redirect_to(@category) }
  format.js
end

请注意 .toggle!将绕过模型验证并保存。如果您需要确保验证运行,您可以使用 @category.toggle(:is_active).save

这将响应format.js并调用一个名为toggle_is_active.js.erb的非常小的文件:

$('#<%= @category.id %>').html('<%= escape_javascript(render :partial => 'toggle_active') %>');

它的目标是我们设置为 id 的 html 元素的 id表行中的类别。

这调用了一个名为 _toggle_active.html.erb 的部分并更新相应的<td>使用新的 html:

<% status = @category.is_active? ? 'Active' : 'Inactive' %>
<button class="button_<%= status %>"><%= link_to status, toggle_active_path(:id => @category.id), :method => :post, :remote => true %></button>

现在您所需要的只是访问 AJAX 链接的路由:

路线.rb:

post 'toggle_active' => 'categories#toggle_is_active'

这是一个可以在 github 上克隆的工作示例。它有样式表来获得上面的外观。我认为你可以在几乎任何情况下推断出这一点:

https://github.com/Beartech/category_boxes

关于mysql - 用于编辑集合的 Rails 表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49660671/

相关文章:

ruby-on-rails - 使用 declarative_authorization 保护敏感属性

ruby - 这个 Ruby 拼写错误解析为什么呢?

ruby-on-rails - 无法获得 Ruby 的#any?使用 nil 对象列表返回 false

java - hibernate 将 java Long 映射到 MySQL BIGINT 错误

ruby-on-rails - 如何在事件管理仪表板 - Rails 中呈现另一个资源的表单?

ruby-on-rails - 索引非模型对象以在Elasticsearch中的Rails中进行搜索

ruby - 哪个版本的 ruby​​ bug 更少?

mySQL - 查询行数和总百分比太慢

mysql - MS-Access:表太多,文件太大 - 多个链接表查询?

mysql - 对数据库中的每个表执行一条语句