Ruby on rails 简单表单-将关联集合设置为默认id
我不确定如何着手解决以下问题。任何帮助都将不胜感激Ruby on rails 简单表单-将关联集合设置为默认id,ruby-on-rails,ruby-on-rails-4,Ruby On Rails,Ruby On Rails 4,我不确定如何着手解决以下问题。任何帮助都将不胜感激 我有一个用户的个人资料表明他们来自一个国家,其国家类别id为8 当用户填写申请表时,我希望将country select(国家/地区选择)选项默认设置为用户个人资料中所述的用户来自的国家(国家/地区类别id:8) 在_form.html.erb中,我通过添加“value:current_user.firstname”成功地使用firstname解决了这个问题 问题:您能否建议如何为s simpleform执行此操作 关联选择选项-根据用户国
- 我有一个用户的个人资料表明他们来自一个国家,其国家类别id为8
- 当用户填写申请表时,我希望将country select(国家/地区选择)选项默认设置为用户个人资料中所述的用户来自的国家(国家/地区类别id:8)
- 在_form.html.erb中,我通过添加“value:current_user.firstname”成功地使用firstname解决了这个问题
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
模型
视图: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 %>
我已经有一段时间没有使用rails了,但我正在尝试提供帮助 您是否尝试过插入
:selected=>当前用户。类别\u国家\u 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 %>
current\u user.category\u country.name,:selected=>current\u user.category\u country\u id%>
我已经有一段时间没有使用rails了,但我正在尝试提供帮助
您是否尝试过插入:selected=>当前用户。类别\u国家\u 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 %>
current\u user.category\u country.name,:selected=>current\u user.category\u country\u id%>
你太棒了!!非常感谢您current\u user.category\u country.name,:selected=>current\u user.category\u country\u id%>
您太棒了!!非常感谢current\u user.category\u country.name,:selected=>current\u user.category\u country\u 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 %>