collection_select有多个模型rails 4

时间:2015-11-20 19:13:05

标签: ruby-on-rails

我有产品和书籍。我正在尝试使用collection_select

将图书连接到产品

以下是我的产品形式的一部分:

    <%= f.label :book_id %><br>
    <%= f.collection_select(:book_id, Book.all, :id, :title, {}, multiple: :true) %>

图书模型:

class Book < ActiveRecord::Base
 belongs_to :product
end

产品型号:

class Product < ActiveRecord::Base
 has_many :books
end

迁移:

class AddBookToProducts < ActiveRecord::Migration
 def change
  add_column :products, :book_id, :integer
  add_index :products, :book_id
 end
end

class AddProductToBooks < ActiveRecord::Migration
 def change
  add_column :books, :product_id, :string
  add_index :books, :product_id
 end
end

BooksController中:

def new
 @book = Book.new
 authorize @book
end

def create
 @book = Book.new(book_params)
 @book.user = current_user
...

的ProductsController:

        class ProductsController < ApplicationController
          require "stripe"
          before_action :set_product, only: [:show, :edit, :update, :destroy]

          # GET /products
          # GET /products.json
          def index
            @products = Product.all.page params[:page]
            authorize @products

          end

          # GET /products/1
          # GET /products/1.json
          def show

           @stripe_btn_data = {
             key: "#{ Rails.configuration.stripe[:publishable_key] }",
             description: @product.title,
             amount: (@product.price * 100),
           }    


           authorize @product
          end

          # GET /products/new
          def new
            @product = Product.new
            @books = Book.all
            authorize @product
          end

          # GET /products/1/edit
          def edit
            authorize @product
          end

          # POST /products
          # POST /products.json
          def create
            @product = Product.new(product_params)
            authorize @product

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

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

          # DELETE /products/1
          # DELETE /products/1.json
          def destroy

            @product.destroy
            authorize @product
            respond_to do |format|
              format.html { redirect_to products_url, notice: 'Product was successfully destroyed.' }
              format.json { head :no_content }
            end
          end

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

            # Never trust parameters from the scary internet, only allow the white list through.
            def product_params
              params.require(:product).permit(:title, :body, :price, :active, :state, :hardcopy_book, :online_only, :online_hardcopy, :expiration, :monthly, :annual, :user_id, :book, :book_id, :id)
            end
        end

参数显示:book_id但不会将book_id插入到产品中。

以下是我在产品/展示

上展示所选书籍的方法
  <strong>Book:</strong>
  <ul>
   <% @product.books.each do |book| %>
    <li><%= link_to book.tile, (book) %><li>
   <% end %>
  </ul>

我错过了什么?

1 个答案:

答案 0 :(得分:1)

首先,您的迁移是错误的,我们只需要一次迁移,这里是:

LPSTR

因为Book属于Product而且Product有很多Book,所以在Books表中添加一列class AddBookToProducts < ActiveRecord::Migration def change add_column :books, :product_id, :string add_index :books, :product_id end end 就足够了。

其次,请将您的“商品”表单修改为:(请注意从product_idbook_id的更改)

book_ids

然后将<%= f.label :book_id %><br> <%= f.collection_select(:book_ids, Book.all, :id, :title, {}, multiple: :true) %> 添加到ProductsController

中的Strong参数
book_ids