我不确定如何解决以下问题。任何帮助将不胜感激
非常感谢问题:可以建议如何对简单形式进行此操作 关联选择选项 - 根据用户国家/地区ID
将国家/地区选择选项设置为默认ID
模式
create_table "users", force: true do |t|
t.string "firstname"
t.string "lastname"
t.integer "number"
t.string "email"
t.text "language"
t.integer "category_country_id"
end
create_table "forms", force: true do |t|
t.string "firstname"
t.string "lastname"
t.integer "number"
t.string "email"
t.integer "category_country_id"
end
create_table "category_countries", force: true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
模型
User belongs_to category_country
Form belongs_to category_country
CategoryCountry has_many users
CategoryCountry has_many forms
views:forms / _form.html.erb
<%= f.input_field :firstname, value: current_user.firstname %>
<%= f.association :category_country, collection: CategoryCountry.all, prompt: "select a category", label: "current country (you currently live in)", :value => current_user.category_country.name %>
forms_controller.rb
def new
@user = current_user
@form = Form.new
end
def create
@form = Form.new(form_params)
@form.user_id = current_user.id
if @form.save
redirect_to application_submitted_path
else
redirect_to user_advert_path(advert.user, advert)
end
end
答案 0 :(得分:1)
我暂时没有使用过rails,但我正在努力提供帮助。
您是否尝试过插入:selected => current_user.category_country_id
<%= f.association :category_country, collection: CategoryCountry.all, prompt: "select a category", label: "current country (you currently live in)", :value => current_user.category_country.name, :selected => current_user.category_country_id %>