Ruby on rails 如何在“选择”菜单的“编辑”页面中获取选定选项

Ruby on rails 如何在“选择”菜单的“编辑”页面中获取选定选项,ruby-on-rails,select,Ruby On Rails,Select,这是我在ruby中的select标记,提交表单后,我想编辑所选选项。因此,我在我的edit.htm.erb中完成了此操作,但当我单击“编辑”按钮时,我提交的选项不会出现在“选择”字段中。它显示选择一个类别 请给我一个解决办法 form.html.erb <%= simple_form_for([:coaches, @programme]) do |f| %> <%= f.input :title %> <%= select_tag 'category', op

这是我在ruby中的select标记,提交表单后,我想编辑所选选项。因此,我在我的edit.htm.erb中完成了此操作,但当我单击“编辑”按钮时,我提交的选项不会出现在“选择”字段中。它显示选择一个类别

请给我一个解决办法 form.html.erb

<%= simple_form_for([:coaches, @programme]) do |f| %>
<%= f.input :title %>
   <%= select_tag 'category', options_from_collection_for_select(@categories, 'id', 'name',@categories.category_id), :class => "wrapper-dropdown-3_1", :onchange => 'update_subscategories_div(this.value)', prompt: "Select a Category" %>               
 <%= f.button :submit, "PUBLISH", :class => "btn_style" %>
edit.html.erb


您需要将所选元素的id作为\u select方法的\u集合\u中的选项\u中的第三个参数传递

<%= select_tag 'category', options_from_collection_for_select(@categories, 'id', 'name',category_id), :onchange => 'update_subscategories_div(this.value)', prompt: "Select a Category" %>

是否可以粘贴完整的表单代码?@ROR Developeredited@sree:您能粘贴程序模型的所有属性吗?@Aman Garg i添加了模型。它显示错误“未定义的局部变量或方法类别\u id”(类别\u id)表示存储在表中的属性。例如,如果您要将其保存在用户表中并拥有用户实例,则它将是@user.category\u id。@请检查已编辑的代码……您尚未编辑该代码。此部分中会出现错误。输入:category,as::select,options\u For_select@categories,'id','name',f.object.category。我通过删除prompt和classnow检查了itv,它正在工作,但没有编辑字段选项NotAppers。显示选择类别您要在其中存储类别id的属性名称是什么?
@categories = Category.all.map{|c| [c.name, c.id]}

<%= simple_form_for([:coaches, @programme]) do |f| %>
  <%= f.input :title %>
  <%= f.input :category, as: :select, collection: @categories, selected: f.object.category, input_html: { class: 'wrapper-dropdown-3_1'}, :onchange => 'update_subscategories_div(this.value)', include_blank: "Select a Category" %>
  <%= f.button :submit, "PUBLISH", :class => "btn_style" %>
<% end %>