如何在表单Rails中添加新字段?

时间:2019-06-07 14:07:27

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-5

我正在尝试使用Rails向表单添加新字段。每个字段都需要有一个运输选项的下拉菜单,其值是:shipping_option_id:

如何使用表单?

模式

 create_table "products", force: :cascade do |t|
    t.string "sku"
    t.string "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.bigint "shipping_option_id"
    t.bigint "product_region_id"
    t.index ["product_region_id"], name: "index_products_on_product_region_id"
    t.index ["shipping_option_id"], name: "index_products_on_shipping_option_id"
    t.index ["sku"], name: "index_products_on_sku", unique: true
  end

1 个答案:

答案 0 :(得分:0)

您还可以使用助手options_for_selectselect_tag中添加给定模型的值

<%= select_tag 'shipping_option_id', options_for_select(@your_model.collect{ |o| [o.name, o.id] }) %>

来源:https://apidock.com/rails/ActionView/Helpers/FormTagHelper/select_tag