一对多:一个用户有多个存储.一家商店有很多产品



我正在学习ROR。尝试构建此模型:一个用户可以拥有许多商店,每个商店可以拥有许多产品。我已经能够创建链接到其所有者的商店,但我无法为产品做同样的事情。

商店.rb

class Store < ApplicationRecord
  belongs_to :user
  has_many :products, :foreign_key => :store_id
end

产品.rb

class Product < ApplicationRecord
  belongs_to :store
end

products_controller.rb

class ProductsController < ApplicationController
  before_action :set_product, only: [:show, :edit, :update, :destroy]
  # GET /products
  # GET /products.json
  def index
    @products = Product.all
  end
  # GET /products/1
  # GET /products/1.json
  def show
  end
  # GET /products/new
  def new
  end
  # GET /products/1/edit
  def edit
  end
  # POST /products
  # POST /products.json
  def create
    @product = Product.new(product_params)
    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
    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
    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(:product_name, :product_price, :product_description, :product_tag, :sku_code)
    end
end

stores_controller.rb

class StoresController < ApplicationController
  before_action :set_store, only: [:show, :edit, :update, :destroy]
  # GET /stores
  # GET /stores.json
  def index
    @stores = Store.all
  end
  # GET /stores/1
  # GET /stores/1.json
  def show
    @products = Product.all
  end
  # GET /stores/new
  def new
    @store = Store.new
  end
  # GET /stores/1/edit
  def edit
  end

  # POST /stores
  # POST /stores.json
  def create
    @store = Store.new(store_params)
    @store.user_id = current_user.id
    respond_to do |format|
      if @store.save
        format.html { redirect_to @store, notice: 'Store was successfully created.' }
        format.json { render :show, status: :created, location: @store }
      else
        format.html { render :new }
        format.json { render json: @store.errors, status: :unprocessable_entity }
      end
    end
  end
  # PATCH/PUT /stores/1
  # PATCH/PUT /stores/1.json
  def update
    respond_to do |format|
      if @store.update(store_params)
        format.html { redirect_to @store, notice: 'Store was successfully updated.' }
        format.json { render :show, status: :ok, location: @store }
      else
        format.html { render :edit }
        format.json { render json: @store.errors, status: :unprocessable_entity }
      end
    end
  end
  # DELETE /stores/1
  # DELETE /stores/1.json
  def destroy
    @store.destroy
    respond_to do |format|
      format.html { redirect_to stores_url, notice: 'Store was successfully destroyed.' }
      format.json { head :no_content }
    end
  end
  private
    # Use callbacks to share common setup or constraints between actions.
    def set_store
      @store = Store.find(params[:id])
    end
    # Never trust parameters from the scary internet, only allow the white list through.
    def store_params
      params.require(:store).permit(:store_name, :store_description)
    end
end

我希望在创建产品时,它会立即获得store_id。我不断收到"商店必须存在"消息

从 Rails 5 开始,belongs_to 要求存在关联对象。如果product对象没有有效的store_id(nil或不存在的存储对象(,则该对象将无效product

您可以通过允许关联是可选的来绕过这一点

class Product < ApplicationRecord
  belongs_to :store, optional: true
end

或者,您应该允许store_id products_controller中的允许参数,并在创建新产品时传递它

# below code I assume that your store table has a column called 'store_name'
<%= form_for @product do |f| %>
  <%= f.collection_select :store_id, current_user.stores, :id, :store_name, prompt: 'Please select the store of this product' %>
  <%= f.submit %>
<% end %>

最新更新