下拉列表在 Rails 中保存 id 而不是 name

Drop down List saves id instead of name in Rails

我有 3 个级联下拉列表。用户可以 select 模型品牌和年份并将其保存到船模型中。用户有很多船。船属于用户。

我猜问题出在 #create 操作上。因为它将 id 而不是名称保存到 Boat Model(Brand Model Year(所有字符串))。

这是我的控制器;

def new
    @boat = Boat.new
  end

  def create
   @boat = current_user.boats.new(boat_params) if logged_in?
    if @boat.save
      flash[:success] = "Boat created!"
      render 'edit'
    else
      render 'new' 
    end
  end

  def show
  end


  def edit
    @boat = Boat.find(params[:id])
  end


  def update
  end


  def update_years
    # updates year and model based on brand selected
    brand = Brand.find(params[:brand_id])
    # map to name and id for use in our options_for_select
    @years = brand.years.map{|a| [a.name, a.id]}.insert(0, "Select a Year")
    @models   = brand.models.map{|s| [s.name, s.id]}.insert(0, "Select a Model")
  end

  def update_models
    # updates model based on year selected
    year = Year.find(params[:year_id])
    @models = year.models.map{|s| [s.name, s.id]}.insert(0, "Select a Model")
  end
end

private

    def boat_params
      params.require(:boat).permit(:brand, :year, :model)
    end

这是我的 #new 观点;

<% provide(:title, 'List My Boat') %>
<div class="container">
  <div class="row">
    <div class="col-md-6 col-md-offset-3">
    <%= form_for(@boat) do |f| %>
      <%= render 'shared/error_messages', object: f.object %>
      <div class="col-md-6">
      <%= f.label :Brand %>
      <%= f.collection_select(:brand,  Brand.all,  :id, :name, {:prompt   => "Select a Brand"}, {:id => 'brands_select'}) %>
    </div>
    <div class="col-md-6">
      <%= f.label :Year %>
      <%= f.collection_select(:year, Year.all, :id, :name, {:prompt   => "Select a Year"}, {:id => 'years_select'}) %>
       </div>
      <div class="col-md-6">
      <%= f.label :Model %>
      <%= f.collection_select(:model, Model.all, :id, :name, {:prompt   => "Select a Model"}, {:id => 'models_select'}) %>
     </div>
      <div class="col-md-6 col-md-offset-3">
      <%= f.submit "Next", class: "btn btn-primary"%>
     </div>
    <% end %>

    </div>
  </div>
</div>

<script>
  $(document).ready(function() {
    $('#brands_select').change(function() {
      $.ajax({
        url: "<%= update_years_path %>",
        data: {
          brand_id : $('#brands_select').val()
        },
        dataType: "script"
      });
    });
    $('#years_select').change(function() {
      $.ajax({
        url: "<%= update_models_path %>",
        data: {
          year_id : $('#years_select').val()
        },
        dataType: "script"
      });
    });
  });
</script>

最后,如果我在这里更改id

<%= f.collection_select(:brand,  Brand.all,  :id, :name, {:prompt   => "Select a Brand"}, {:id => 'brands_select'}) %>

要命名,那么级联属性就不行了。所以表单不会动态更新自己。

我尝试过类似 #create 操作

  year =  Year.find_by("id = ?", params[:year])
  model =  Year.find_by("id = ?", params[:model])
  brand =  Year.find_by("id = ?", params[:brand])
  @boat = current_user.boats.new(:year, year.name, :brand, brand.name, :model, model.name) 

但是由于@boat = current_user.boats.new(:year, year.name, :brand, brand.name, :model, model.name)是错误的,所以它给出了一个错误。我收到

undefined method `name' for nil:NilClass

错误。

编辑 1:

update_models.js.erb
$('#models_select').html("<%= escape_javascript(options_for_select(@models)) %>");

update_years.js.erb
$('#years_select').html("<%= escape_javascript(options_for_select(@years)) %>");
$('#models_select').html("<%= escape_javascript(options_for_select(@models)) %>");

如果您查看 rails collection_select method,它是作为参数传递的值方法,在您的情况下,它是年份的 ID 和品牌.

将您的 collection select 方法更改为:

<%= f.collection_select(:brand,  Brand.all,  :name, :name, {:prompt   => "Select a Brand"}, {:id => 'brands_select'}) %>
<%= f.collection_select(:year, Year.all, :name, :name, {:prompt   => "Select a Year"}, {:id => 'years_select'}) %>

您的脚本应该是:

<script>
  $(document).ready(function() {
    $('#brands_select').change(function() {
      $.ajax({
        url: "<%= update_years_path %>",
        data: {
          brand_name : $('#brands_select').val()
        },
        dataType: "script"
      });
    });
    $('#years_select').change(function() {
      $.ajax({
        url: "<%= update_models_path %>",
        data: {
          year_name : $('#years_select').val()
        },
        dataType: "script"
      });
    });
  });
</script>

在您的方法中您需要按名称而不是 ID 查找品牌和年份

def update_years
  # updates year and model based on brand selected
  brand = Brand.find_by_name(params[:brand_name])
  # map to name and id for use in our options_for_select
  @years = brand.years.map{|a| [a.name, a.name]}.insert(0, "Select a Year") #use a.name here instead of a.id
  @models   = brand.models.map{|s| [s.name, s.name]}.insert(0, "Select a Model")#use s.name here instead of s.id
end

def update_models
  # updates model based on year selected
  year = Year.find_by_name(params[:year_name])
  @models = year.models.map{|s| [s.name, s.name]}.insert(0, "Select a Model") #use s.name here instead of s.id
end