【问题标题】:Rails HABTM checkboxes don't save to databaseRails HABTM 复选框不保存到数据库
【发布时间】:2011-09-18 22:50:21
【问题描述】:

我正在开始 Rails 编程,我想要完成的只是拥有一个复选框列表,我可以从中为产品选择相应的类别。 我在这件事上关注了 railscast,但它似乎对我不起作用。

我使用 Rails 3.0.9 和 Ruby 1.8.7。

我已正确设置模型以及连接表的迁移。 当我尝试编辑产品时,category_ids 被发布,但它们从未保存到数据库中。

日志:

Started POST "/products/1" for 127.0.0.1 at Mon Sep 19 01:39:33 +0300 2011
  Processing by ProductsController#update as HTML
  Parameters: {"commit"=>"Update Product",     "authenticity_token"=>"x2Z5GGiNh9yLz6xjuQaMqdGW3Gw7dYe8dSghyrFpiYk=", "utf8"=>"���", "id"=>"1", "product"=>{"name"=>"Magicianulx2", "price"=>"121", "category_ids"=>["1"]}}
  Product Load (0.1ms)  SELECT "products".* FROM "products" WHERE "products"."id" = 1 LIMIT 1
WARNING: Can't mass-assign protected attributes: category_ids
Redirected to http://localhost:3000/products/1
Completed 302 Found in 10ms


Started GET "/products/1" for 127.0.0.1 at Mon Sep 19 01:39:33 +0300 2011
  Processing by ProductsController#show as HTML
  Parameters: {"id"=>"1"}
  Product Load (0.1ms)  SELECT "products".* FROM "products" WHERE "products"."id" = 1 LIMIT 1
Rendered products/show.html.erb within layouts/application (4.2ms)
Completed 200 OK in 11ms (Views: 6.1ms | ActiveRecord: 0.2ms)

型号:

class Product < ActiveRecord::Base
  attr_accessible :name, :price
  has_and_belongs_to_many :categories
end

class Category < ActiveRecord::Base
  attr_accessible :name
  has_and_belongs_to_many :products
end

迁移:

class AddCategoriesProducts < ActiveRecord::Migration
  def self.up
    create_table :categories_products, :id => false do |t|
      t.integer :category_id
      t.integer :product_id
    end
  end

  def self.down
    drop_table :categories_products
  end
end

控制器(产品控制器中的更新方法)

  def update
    @product = Product.find(params[:id])
    if @product.update_attributes(params[:product])
      redirect_to @product, :notice  => "Successfully updated product."
    else
      render :action => 'edit'
    end
  end

视图/产品中的我的 _form.html.erb

<%= form_for @product do |f| %>
  <%= f.error_messages %>
  <p>
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </p>
  <p>
    <%= f.label :price %><br />
    <%= f.text_field :price %>
  </p>

<% for category in Category.find(:all) %>
        <div>
                <%= check_box_tag "product[category_ids][]", category.id, @product.categories.include?(category) %>
                <%= category.name %>
        </div>
   <% end %>

<p><%= f.submit %></p>
<% end %>

控制器和视图都是使用漂亮的脚手架生成的。 其他一切似乎都可以正常工作,但复选框中的值不会保存到数据库中。 通过 rails 控制台手动添加它们就可以了。 关于我做错了什么有什么想法吗?

【问题讨论】:

    标签: ruby-on-rails checkbox has-and-belongs-to-many railscasts


    【解决方案1】:

    您的产品模型有attr_accessible :name, :price,这就是为什么当您尝试在控制器中执行@product.update_attributes(params[:product]) 时日志显示WARNING: Can't mass-assign protected attributes: category_ids

    尝试将category_ids 添加到attr_accessible

    【讨论】:

    • @James,如果你觉得无聊,看看我的博客文章,告诉我为什么我不需要attr_accessible,因为它一直在困扰着我,我还没有有机会重温它:/
    【解决方案2】:

    当我做或多或少相同的事情时,我的check_box_tag 看起来不同了:

    <%= check_box_tag :category_ids,  
                      cat.id,  
                      @product.categories.include?(cat),  
                      :name => 'product[category_ids][]' %>  
    

    我最终不需要需要attr_accessible,尽管正如我在my blog post describing the exact same thing 中提到的,我仍然不确定为什么不。我也用过has_many:through

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-14
      • 1970-01-01
      • 1970-01-01
      • 2016-05-19
      • 1970-01-01
      • 2012-10-19
      • 1970-01-01
      • 2016-01-15
      相关资源
      最近更新 更多