【发布时间】:2016-07-12 06:54:39
【问题描述】:
您好,我是 ruby on rails 的新手。我创建了两个模型品牌和产品。一个品牌可以与许多产品相关联。我使用了脚手架,因为这是我们的讲师希望我们在项目中做的事情。
我已尝试设置我的产品屏幕,以便在您输入产品时获得可供选择的品牌名称下拉列表。输入商品详情后,选择品牌名称,点击提交;所选乐队名称中的brand_id 保存到product.brand_id 表中。 (我相信当我在控制器中设置 has_many 时,brand_id 会自动在 products 表中创建)。
我曾尝试使用 collection_set 执行此操作,但我苦苦挣扎。我曾尝试使用一些关于堆栈溢出的技巧,但它们都没有奏效,作为最后的手段,我尝试自己发布一个帖子。
在模型中我添加了以下内容:
class Brand < ApplicationRecord
has_many :product
validates :brand_name, presence: true
end
class Product < ApplicationRecord
belongs_to :brand
validates :name, presence: true
end
据我了解,这意味着产品表将具有品牌 ID 和 ID。
在产品控制器中,我添加了以下内容:
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
@brand = Brand.all
@product = Product.new
end
... more code ...
在产品视图的表单中,我添加了以下内容
<%= form_for(product) do |f| %>
<% if product.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(product.errors.count, "error") %> prohibited this product from being saved:</h2>
<ul>
<% product.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :model %>
<%= f.text_field :model %>
</div>
<div class="field">
<%= f.label :price %>
<%= f.text_field :price %>
</div>
<div class="field">
<%= f.label :vat %>
<%= f.text_field :vat %>
</div>
<div class="field">
<%= f.label :image_uri %>
<%= f.text_field :image_uri %>
</div>
<div class="field">
<%= f.collection_select :brand_id, @brand, :id, :brand_name %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
routes.rb
Rails.application.routes.draw do
resources :users
resources :products
resources :sales_orders
resources :payments
resources :filter_group_items
resources :filter_groups
resources :categories
resources :sales_order_lines
resources :brands
resources :addresses
get '/cart' => 'cart#index'
get '/products' => 'products#index'
get '/brands' => 'brands#index'
get '/users' => 'users#index'
get '/addresses' => 'addresses#index'
get '/payments' => 'payments#index'
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
#root 'application#hello'
# You can have the root of your site routed with "root"
#root 'welcome#index'
root 'products#index'
end
我收到以下错误 显示 /home/ubuntu/workspace/ssd_project/app/views/products/_form.html.erb 其中第 40 行提出:
用于#的未定义方法`brand_id' 你的意思?品牌
39 <div class="field">
40 <%= f.collection_select :brand_id, @brand, :id, :brand_name %>
41 </div>
模板包含的痕迹:app/views/products/new.html.erb
如果有人对如何执行此操作有任何建议,我将不胜感激。
新路线.rb
Rails.application.routes.draw do
resources :users
resources :products
resources :sales_orders
resources :payments
resources :filter_group_items
resources :filter_groups
resources :categories
resources :sales_order_lines
resources :brands
resources :addresses
resources :brands do
resources :products
end
get '/cart' => 'cart#index'
get '/products' => 'products#new'
get '/brands' => 'brands#index'
get '/users' => 'users#index'
get '/addresses' => 'addresses#index'
get '/payments' => 'payments#index'
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
#root 'application#hello'
# You can have the root of your site routed with "root"
#root 'welcome#index'
root 'products#index'
end
数据库架构 schema.rb
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20160706122948) do
create_table "addresses", force: :cascade do |t|
t.string "line1"
t.string "line2"
t.string "line3"
t.string "country"
t.string "post_code"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "brands", force: :cascade do |t|
t.string "brand_name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "categories", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "filter_group_items", force: :cascade do |t|
t.string "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "filter_groups", force: :cascade do |t|
t.string "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "payments", force: :cascade do |t|
t.datetime "date"
t.decimal "amount"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "products", force: :cascade do |t|
t.string "name"
t.string "model"
t.decimal "price"
t.decimal "vat"
t.string "image_uri"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "sales_order_lines", force: :cascade do |t|
t.integer "qty"
t.decimal "price"
t.decimal "total"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "sales_orders", force: :cascade do |t|
t.datetime "date"
t.decimal "total"
t.decimal "vat"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "users", force: :cascade do |t|
t.string "name"
t.string "email"
t.string "password"
t.string "phone"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "admin_user", default: false
end
end
【问题讨论】:
标签: ruby-on-rails ruby