使用rails form_for来检索记录而不是创建它们

时间:2013-10-24 05:05:19

标签: ruby-on-rails ajax forms

我有自行车模型:

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

我正在尝试在我的应用主页上实现一个表单,该表单有三个collection_select输入(后两个是基于先前的collection_select使用AJAX的选定值动态填充的),用于制作,模型和年份。表单本身呈现并且所有AJAX请求都在起作用。

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

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

<%= 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。我对如何通过控制器获取记录感到困惑。

1 个答案:

答案 0 :(得分: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

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