【发布时间】:2015-02-12 13:54:35
【问题描述】:
我正在尝试为我的 rails 应用程序提交表单,但标题中出现错误,我不完全确定如何修复它。类别是我的外键。
= form_for @menu_price do |f|
- if @menu_price.errors.any?
#error_explanation
h2 = "#{pluralize(@menu_price.errors.count, "error")} prohibited this menu_price from being saved:"
ul
- @menu_price.errors.full_messages.each do |message|
li = message
.form-group
= f.label :category
= f.select :category, options_for_select(@categories.map{ |c| [c.name] })
.form-group
= f.label :price
= f.number_field :price
.form-group
= f.label :description
= f.text_field :description
.form-group
= f.label :serves
= f.text_field :serves
= f.submit
= link_to 'Back', menu_prices_path, class:'button'
我的模型是这样的
class Category < ActiveRecord::Base
has_many :menu_prices
validates :category, :presence => true
end
***********更新***********
class CreateMenuPrices < ActiveRecord::Migration
def change
create_table :menu_prices do |t|
t.text :description, :null => false
t.decimal :price , :default => nil , :null => true
t.string :serves, :default => nil , :null => true
t.integer :small , :default => nil , :null => true
t.integer :regular, :default => nil , :null => true
t.integer :large, :default => nil , :null => true
t.integer :party, :default => nil, :null => true
t.timestamps null: false
end
add_reference :menu_prices, :categories
end
end
我知道它需要一个外键,但我不确定如何在表单中提交外键。任何帮助将不胜感激。 ******更新****** 我的 schema.rb 在下面
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 "menu_prices", force: :cascade do |t|
t.text "description", null: false
t.decimal "price"
t.string "serves"
t.integer "small"
t.integer "regular"
t.integer "large"
t.integer "party"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "categories_id"
end
end
【问题讨论】:
-
不确定是否重要,但我使用的是 rails V4.2
-
我们可以得到完整的回溯吗?
-
您是否尝试从收到的参数中加载控制器中的类别?
-
@Nerian 我不确定我会在哪里发帖
-
类似于“@category = Category.find(menu_price_params[:category])”的 [可能] MenuPrice#create 控制器操作
标签: ruby-on-rails forms slim-lang