Ruby on rails 下拉列表在Rails中保存id而不是名称

Ruby on rails 下拉列表在Rails中保存id而不是名称,ruby-on-rails,controller,cascading,Ruby On Rails,Controller,Cascading,我有3个下拉列表。用户可以选择模型品牌和年份,并保存到船模型。用户有很多船。船属于使用者 我想问题在于创造行动。因为它将ID而不是名称保存到船模品牌车型年份所有字符串中 这是我的控制器 def new @boat = Boat.new end def create @boat = current_user.boats.new(boat_params) if logged_in? if @boat.save flash[:success] = "Boat

我有3个下拉列表。用户可以选择模型品牌和年份,并保存到船模型。用户有很多船。船属于使用者

我想问题在于创造行动。因为它将ID而不是名称保存到船模品牌车型年份所有字符串中

这是我的控制器

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
这是我的新观点

但是由于@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,它是作为params传递的value方法,在你的例子中,它是年份和品牌的id

将集合选择方法更改为:

<%= 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'}) %>

但在这种情况下,模型不会动态更新自身。最后一滴down@Shalafister那是另一个问题。当用户选择一个选项时,您需要查看如何更新select。在这里,我得到了错误:BoatsController更新_models undefined method models'for nil:NilClass`。我添加了js文件。谢谢这里是“网络”选项卡上的,它将19作为id而不是年份名称,这就是为什么第3款下拉列表没有自动更新的原因。但是为什么会发生呢,,https://........................../boats/update_models?year_name=19&_=1429438923235it 很奇怪,我添加了js文件。我不是一个js专家,但对我来说似乎没什么错。为什么你想要名称而不是id?因为3个下拉列表是级联的,它们是关联的。因此,当用户选择模型品牌和年份时,它将获取名称并将其保存到与用户模型关联的另一个模型中。也许有不同的方法,但这就是我脑海中闪现的。我对其他想法持开放态度。
undefined method `name' for nil:NilClass
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)) %>");
<%= 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>
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