0

我正在开始 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 控制台手动添加它们就可以了。关于我做错了什么的任何想法?

4

2 回答 2

2

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

尝试添加category_idsattr_accessible.

于 2011-09-18T23:05:47.880 回答
0

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

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

我最终不需要attr_accessible尽管正如我在描述完全相同的事情的博客文章中提到的那样,我仍然不确定为什么不。我也用过has_many:through

于 2011-09-18T23:12:30.737 回答