2013-10-24 15 views
0

我的自行车型号:使用Rails的form_for检索记录不能创建

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

我想实现我的应用程序的主页的形式,有三个collection_select输入(后两个都是动态填充基于以前的collection_select使用AJAX的选定值)作为品牌,型号和年份。表单本身呈现并且所有的AJAX请求都在工作。

提交后,我想呈现与用户选择相匹配的现有自行车模型记录的展示页面(基于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 %> 

bikes_controller

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。我很困惑如何通过控制器获取记录。

+0

你能提供共享文件,如:year_questions_fields和model_questions_fields?添加了 – Kuldeep

+0

。我不确定它们是否相关 - 我感到困惑的是form_for的设置。 – IkegawaTaro

+0

你正在运行什么版本的导轨?你可以发布你的自行车控制器吗? – oobie11

回答

1

不知道这是最有效的或最好的方式,但我可以用下面的代码来解决这个问题:

bikes_controller

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 

我被错误地访问参数(它们被嵌套)。

相关问题