在create action中从ids获取名称的方法

时间:2015-04-17 12:01:56

标签: ruby-on-rails controller action cascading

所以我有3个级联下拉列表 - 品牌,年份和型号。而且我有一个船模型。但船模型与3级联下降无关。用户选择下降并导航#create船。问题是我有下拉所选项目的ID。我的#create是;

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

boat_params

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

当我点击下一个按钮时,由于我相信的#create动作,它会保存它。

以下是#new.html

<% 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>

以下是完整的#boats controller

class BoatsController < ApplicationController


  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

private

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


end

1 个答案:

答案 0 :(得分:1)

这是因为f.collection_select(...):id作为品牌,型号和年份的关键。如果您坚持使用:name作为密钥,请尝试以下操作:

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