首页 > 解决方案 > 如何使用下拉列表从独立页面设置 id?或者我如何保存belongs_to、has_many 值?

问题描述

我是 ruby​​ on rails 的初学者。我想创建品牌 - 产品列表。我想从独立页面添加产品名称和品牌名称(应在下拉列表中选择)。每个产品都有名称和品牌 ID。ı将从下拉列表中选择的品牌名称设置为变量。之后,我会将用户在独立页面中写入的产品名称和品牌 ID 添加到产品表中。

路线.rb

Rails.application.routes.draw do
  get 'welcome/index'
  get 'add_product/index'
  # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
  resources :brands do
    resources :products
  end
  root 'welcome#index'

end

.._create_brands.rb

class CreateBrands < ActiveRecord::Migration[6.0]
  def change
    create_table :brands do |t|
      t.string :title

      t.timestamps
    end
  end
end

.._create_products.rb

class CreateProducts < ActiveRecord::Migration[6.0]
  def change
    create_table :products do |t|
      t.string :name
      t.references :brand, null: false, foreign_key: true

      t.timestamps
    end
  end
end

品牌.rb

class Brand < ApplicationRecord
  has_many :products, dependent: :destroy
  validates :title, presence: true,
                    length: { minimum: 2 }
end

产品.rb

class Product < ApplicationRecord
  belongs_to :brand
end

add_product/index.html.erb (add_product 有视图和控制器)

<h1>Add a new product</h1>
<%= form_with(model: @product) do |f| %>
<p>
  <%= f.label :name,"Product name: " %><br>
  <%= f.text_field :name %>
</p>
<%= f.collection_select(:brand_id, Brand.all, :id, :title) %>
<p>

    <%= f.submit "Add a product" %>

</p>
<% end %>

add_product_controller.rb

class AddProductController < ApplicationController
  def index
  end

  def new
   @product = Product.new
  end

   def create
     @product = Product.new(product_params)
     if @product.save
        redirect_to @product
     else
        render :new
     end
   end

   private
     def product_params
       params.require(:product).permit(:name,:brand_id)
     end

end

当我在控制台中运行 rails 路线时,

 Prefix Verb   URI Pattern                                                                              Controller#Action
                        welcome_index GET    /welcome/index(.:format)                                                                 welcome#index
                    add_product_index GET    /add_product/index(.:format)                                                             add_product#index
                       brand_products GET    /brands/:brand_id/products(.:format)                                                     products#index
                                      POST   /brands/:brand_id/products(.:format)                                                     products#create
                    new_brand_product GET    /brands/:brand_id/products/new(.:format)                                                 products#new
                   edit_brand_product GET    /brands/:brand_id/products/:id/edit(.:format)                                            products#edit
                        brand_product GET    /brands/:brand_id/products/:id(.:format)                                                 products#show
                                      PATCH  /brands/:brand_id/products/:id(.:format)                                                 products#update
                                      PUT    /brands/:brand_id/products/:id(.:format)                                                 products#update
                                      DELETE /brands/:brand_id/products/:id(.:format)                                                 products#destroy
                               brands GET    /brands(.:format)                                                                        brands#index
                                      POST   /brands(.:format)                                                                        brands#create
                            new_brand GET    /brands/new(.:format)                                                                    brands#new
                           edit_brand GET    /brands/:id/edit(.:format)                                                               brands#edit
                                brand GET    /brands/:id(.:format)                                                                    brands#show
                                      PATCH  /brands/:id(.:format)                                                                    brands#update
                                      PUT    /brands/:id(.:format)                                                                    brands#update
                                      DELETE /brands/:id(.:format)                                                                    brands#destroy
                                 root GET    /                                                                                        welcome#index

独立页面图片

如果我去麸皮并点击节目,我可以添加只有名称的产品。但我想从独立页面添加产品。那我该怎么做呢?我可以从下拉列表中设置brand_id 值吗?我可以保存产品名称和品牌 ID 吗?

标签: ruby-on-railsruby

解决方案


推荐阅读