Ruby on rails 3 如何在rails中获取复选框的返回值

Ruby on rails 3 如何在rails中获取复选框的返回值,ruby-on-rails-3,checkbox,Ruby On Rails 3,Checkbox,我很抱歉,因为这看起来像一个双重职位,但我看到了很多其他线程,我不能理解我正在做的任何事情 我正试着做一个“有”和“有”属于“很多”,但我被卡住了 我设法使表单显示正确的信息,但我不知道如何保存它 我得到: Orb类: class Orb < ActiveRecord::Base attr_accessible :descr, :id, :nome, :orb_type_id, :orbt validates_presence_of :nome, :orb_type_id v

我很抱歉,因为这看起来像一个双重职位,但我看到了很多其他线程,我不能理解我正在做的任何事情

我正试着做一个“有”和“有”属于“很多”,但我被卡住了

我设法使表单显示正确的信息,但我不知道如何保存它

我得到:

Orb类:

class Orb < ActiveRecord::Base
  attr_accessible :descr, :id, :nome, :orb_type_id, :orbt

  validates_presence_of :nome, :orb_type_id
  validates :nome, :uniqueness => true

  belongs_to :orb_type

  has_and_belongs_to_many :books
end
这样,表单就有了一个带有正确值的复选框,但它不会被保存。
我不知道我在做什么,有人能给我解释一下我必须做什么吗?

你需要一个连接表来使用has\u和\u属于\u许多人

class CreateTableBooksOrbs < ActiveRecord::Migration
    def change
    create_table :books_orbs, :id => false do |t|
      t.references :orb,  :null => false
      t.references :book, :null => false
    end
    add_index :books_orbs, [:book_id, :orb_id], :unique => true
  end
end
class CreateTableBooksOrbsfalse do\t|
t、 引用:orb,:null=>false
t、 参考文献:book,:null=>false
结束
添加索引:books\u orbs,[:book\u id,:orb\u id],:unique=>true
结束
结束

请参见此处我的工作版本:

为什么我找不到此工作的完整示例?嘿,我尝试按照您所说的更改迁移(我已经创建了它)。我还做了一些其他的事情,比如为设置accepts\u nested\u属性。表单获取标签(图书列表)并正确显示支票簿,但它不会在联合表中保存任何内容!事实上,如果我使用:orb:book,我会得到一个错误:/usr/local/rvm/gems/ruby-1.9.3-p429@global/gems/rake-10.0.4/lib/rake/trace_output.rb:16:in`block in trace_on':US-ASCII中的字节序列无效(ArgumentError)
 <% @book.each do |book| %>
    <div>
      <%= check_box_tag "orb[book_ids][]", book.id, @orb.books.include?(book), id: dom_id(book) %>
      <%= book.nome %>
    </div>
  <% end %>
def new
  @book = Book.all
  @orb = Orb.new
  respond_to do |format|
    format.html # new.html.erb
    format.json { render json: @orb }
  end
end
# GET /orbs/1/edit
def edit
  @orb = Orb.find(params[:id])
  @book = Book.all
end
# POST /orbs
# POST /orbs.json
def create
  @orb = Orb.new(params[:orb])
  respond_to do |format|
    if @orb.save
      format.html { redirect_to @orb, notice: 'save was successful' }
      format.json { render json: @orb, status: :created, location: @orb }
    else
      format.html { render action: "Novo" }
      format.json { render json: @orb.errors, status: :unprocessable_entity }
    end
  end
end
# PUT /orbs/1
# PUT /orbs/1.json
def update
  params[:orb][:book_ids] ||= []
  @orb = Orb.find(params[:id])
  respond_to do |format|
    if @orb.update_attributes(params[:orb])
      format.html { redirect_to @orb, notice: 'save was successful' }
      format.json { head :no_content }
    else
      format.html { render action: "Editar" }
      format.json { render json: @orb.errors, status: :unprocessable_entity }
    end
  end
end
class CreateTableBooksOrbs < ActiveRecord::Migration
    def change
    create_table :books_orbs, :id => false do |t|
      t.references :orb,  :null => false
      t.references :book, :null => false
    end
    add_index :books_orbs, [:book_id, :orb_id], :unique => true
  end
end