Rails 4、Cocoon,ERB Template,如何在Edit Action中选中一个options_from_collection_for_select?

Rails 4, Cocoon, ERB Template, how to make selected an options_from_collection_for_select in Edit Action?

这是我第一次使用 Cocoon,也许这是一个非常愚蠢的问题,但我已经花了很多时间研究如何使用 ERB 模板来做到这一点并避免使用 simple_form 或其他助手。

看看我的模型:

models/loot.rb

class Lot < ActiveRecord::Base

  has_many :colindanciums, dependent: :destroy
  has_many :cardinal_points, through: :colindanciums 
  accepts_nested_attributes_for :colindanciums, allow_destroy: true

end

models/colindancium.rb

class Colindancium < ActiveRecord::Base
    belongs_to :cardinal_poing
    belongs_to :lot
end

models/cardinal_point.rb

class CardinalPoint < ActiveRecord::Base
    has_many :colindanciums
    has_many :lots, through: :colindanciums
end

形式: views/lots/_form.html.erb

<%= form_for(@lot, remote: true) do |f| %>
  <%= render 'shared/error_messages', object: @lot %>
...
...
...

    <fieldset id="colindancium-orientation"> 
      <ol> 
        <%= f.fields_for :colindanciums do |colindancium| %> 
          <%= render 'colindancium_fields', f: colindancium %> 
        <% end %> 
      </ol> 
      <%= link_to_add_association 'Nueva Colindancia', f, :colindanciums, 'data-association-insertion-node' => "#colindancium-orientation ol", 'data-association-insertion-method' => "append" %> 
    </fieldset> 

...
...
...            
<% end %>

部分:

views/lots/_colindancium_fields.html.erb

<li class="control-group nested-fields">
  <div class="controls">
    <%= f.label :description, "Descripcion:" %>
    <%= f.text_field :description %>

    <%= f.label :linear_meters, "Metros Lineales:" %>
    <%= f.text_field :linear_meters %>

    <%= f.label :cardinal_point_id, "Orientacion:" %>
    <%= f.select :cardinal_point_id, 
        options_from_collection_for_select(CardinalPoint.all, :id, :name), { }, { :class => "form-control", :prompt => "Seleccione un Punto Cardinal" } %>

    <%= link_to_remove_association "Eliminar", f %>
  </div>
</li>

当我插入新字段时一切正常,它保存在数据库中,它在数据库中更新它,我的问题是在 options_from_collection_for_select 打开在 Edit Action 中形成,这个助手的第四个参数是 selected 值...我找不到选择存储在我的数据库中的值的方法,它总是显示1 索引...我无法从 _form 访问 @... 对象,其他字段( :description, :linear_meters) 工作得很好我的问题是在 f.select,我不知道该怎么做。

编辑我的控制器:

# GET /lots/new
  def new
    @lot = Lot.new
    @lot.colindanciums.build
    authorize @lot
  end

  # PATCH/PUT /lots/1
  # PATCH/PUT /lots/1.json
  def update
    authorize @lot
    respond_to do |format|
      if @lot.update(lot_params)
        format.html { redirect_to @lot, notice: 'Lot was successfully updated.' }
        format.json { render :show, status: :ok, location: @lot }
        format.js
      else
        format.html { render :edit }
        format.json { render json: @lot.errors, status: :unprocessable_entity }
        format.js { render json: @lot.errors, status: :unprocessable_entity }
      end
    end
  end

你忘记把括号写对了

<%= f.select (:cardinal_point_id, 
    options_from_collection_for_select(CardinalPoint.all, :id, :name), { }, { :class => "form-control", :prompt => "Seleccione un Punto Cardinal" }) %>

我改变了我在 select 中的逻辑,我让它以这种方式工作:

  <div class="form-group">
    <%= f.label :cardinal_point_id, "Orientacion:", :class => "control-label" %>
    <%= f.select :cardinal_point_id , CardinalPoint.all.collect  {|p| [ p.name, p.id ] }, { :include_blank => 'Seleccione un Punto Cardinal'}, :class => "form-control" %>
  </div>

我post我的回答,以防有人遇到同样的问题。