我有以下表格:
<% tb_form_for [:location] do |f| %>
<%= f.tb_text_field :name %>
<%= f.tb_number_field :postal_code %>
<%= f.tb_text_field :phone %>
<%= tb_select :region_id, options_from_collection_for_select(Region.all, :id, :name, :region_id)%>
<% end %>
我理解select的集合选项的细分。位置一是集合,位置二是值,位置三是文本方法,位置为选中。前三个职位工作得很好,这是我能够完全开始工作的选定职位。
例如,如果我的芝加哥位置的id为4.它的伊利诺伊州的区域为6.当我去编辑表格而不是看伊利诺伊州时,我看到加利福尼亚(其ID为4)。
我已经为所选的人做了尝试:
我错过了什么吗?
地区has_many位置。位置belongs_to地区。区域控制器看起来像:
class Admin::RegionsController < Admin::ApplicationController
belongs_to_app :regions
add_breadcrumb 'Regions', :admin_regions_path
before_action :load_region, only: [:show, :edit, :update, :destroy]
def index
@regions = Region.ordered.paginate(page: params[:page])
@regions = @regions.search(params[:search]) if params[:search]
respond_with @regions
end
def show
respond_with @region
end
def new
@region = Region.new
respond_with @region
end
def create
operation = Region::Create.run(region_params)
flash[:notice] = 'Region created successfully' if operation.success
respond_with operation.result, location: admin_regions_path
end
def edit
respond_with @region
end
def update
flash[:notice] = 'Region updated successfully' if @region.update_attributes(region_params)
respond_with @region, location: admin_regions_path
end
def destroy
flash[:notice] = 'Region deleted successfully' if @region.destroy
respond_with @region, location: admin_regions_path
end
private
def load_region
@region = Region.find_by!(id: params[:id])
end
def region_params
params.require(:region).permit(:name, :description, :youtube_url,
:url_name, :staff_photo, :group_photo,
location_ids: [])
end
end
答案 0 :(得分:0)
<%= tb_select :region_id, options_from_collection_for_select(Region.all, :id, :name, @location.region_id)%>