0

我有自行车型号:

class Bike < ActiveRecord::Base
    has_one :make
    has_one :model
    has_one :year

我正在尝试在我的应用程序主页上实现一个表单,该表单具有三个collection_select输入(后两个是根据先前的 collection_select 使用 AJAX 选择的值动态填充的)用于品牌、型号和年份。表单本身呈现并且所有 AJAX 请求都在工作。

提交后,我想呈现与用户选择匹配的现有 Bike 模型记录的显示页面(基于 collection_selects 中所选值的 id)。

这是表格(这是在主页上,而不是自行车的新页面上)。我在自行车控制器中创建了一个搜索操作,但不知道如何将任何数据传递给它:

<%= form_for(:bike, :url => {:controller => 'bikes', :action => 'search'}, :html => {:role => "form", :class => "form-horizontal"}) do |f| %>
        <div class="form-group">
            <%= label :make_id, 'Make' %>
            <%= collection_select(:bike, :make_id, Make.all, :id, :name, {include_blank: true}, {:class=>'form-control'})%>
        </div>

        <div class="form-group">
            <div id="bikeModels"
                <p><%= render 'shared/model_questions_fields', :current_models => [] %></p>
            </div>
        </div>

        <div class="form-group">
            <div id="modelYears"
                <p><%= render 'shared/year_questions_fields', :current_years => [] %></p>
            </div>
        </div>
        <%= f.submit "Show Quote", class: "btn btn-primary btn-lg" %>
    <% end %>

共享/model_questions_fields

<script type="text/javascript">
jQuery(function($) {
// when the #model field changes
  $("#bike_model_id").change(function() {
    // make a POST call and replace the content
    var model = $('select#bike_model_id :selected').val();
    if(model == "") model="0";
    jQuery.get('/bikes/update_year_select/' + model, function(data){
        $("#modelYears").html(data);
    })
    return false;
  });
})
</script>

<%= label :model_id, 'Model' %>
<% if !current_models.blank? %>
    <%= collection_select :bike, :model_id, current_models, :id , :name, {include_blank: true}, {:class=>'form-control'} %>
<% else %>
    <%= collection_select :bike, :model_id, [], :id , :name, {include_blank: true}, {:class=>'form-control'} %>
<% end %>

共享/year_questions_fields

<%= label :year_id, 'Year' %>
<% if !current_years.blank? %>
    <%= collection_select :bike, :year_id, current_years, :id , :year_value, {include_blank: true}, {:class=>'form-control'} %>
<% else %>
    <%= collection_select :bike, :year_id, [], :id , :year_value, {include_blank: true}, {:class=>'form-control'} %>
<% end %>

自行车控制器

class BikesController < ApplicationController
  before_action :set_bike, only: [:show, :edit, :update, :destroy]

  # GET /bikes
  # GET /bikes.json
  def index
    @bikes = Bike.paginate(page: params[:page])
  end

  # GET /bikes/1
  # GET /bikes/1.json
  def show
  end

  # GET /bikes/new
  def new
    @bike = Bike.new
  end

  def search
    @bike = Bike.find_by(:make_id => make_id, :model_id => model_id, :year_id => year_id)
    redirect_to :action => "show", :id => @bike.bike_id
  end

  def update_model_select
    models = Model.where(:make_id=>params[:id]).order(:name) unless params[:id].blank?
    render :partial => "shared/model_questions_fields", :locals => { :current_models => models }
  end

  def update_year_select
    model = Model.find(params[:id])
    render :partial => "shared/year_questions_fields", :locals => { :current_years => model.years }
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_bike
      @bike = Bike.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def bike_params
      params.require(:bike).permit(:year_id, :make, :make_id, :model, :model_id, :kind, 
        :msrp, :current_price, :customer_id, :side_picture, :like_new_value, :excellent_value, :good_value,
        :fair_value)
    end
end

我需要根据用户选择的 make_id、model_id 和年份来获取 bike_id。我对如何通过控制器获取记录感到困惑。

4

1 回答 1

1

不确定这是否是最有效或最好的方法,但我能够使用以下代码解决这个问题:

自行车控制器

def search
    puts params[:bike][:make_id]
    bike = Bike.find_by(:make_id => params[:bike][:make_id], :model_id => params[:bike][:model_id], :year_id => params[:bike][:year_value])
    redirect_to :action => "show", :id => bike.id
end

我错误地访问了参数(它们是嵌套的)。

于 2013-10-24T06:27:59.450 回答