首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用rails form_for检索记录,而不是创建记录

使用rails form_for检索记录,而不是创建记录
EN

Stack Overflow用户
提问于 2013-10-24 05:05:18
回答 1查看 101关注 0票数 0

我有自行车模型:

代码语言:javascript
复制
class Bike < ActiveRecord::Base
    has_one :make
    has_one :model
    has_one :year

我正试图在我的应用程序主页上实现一个表单,其中包含三个collection_select输入(第二个输入是根据以前的集合_select使用AJAX选择的值动态填充的),用于make、model和AJAX。表单本身呈现,所有AJAX请求都正常工作。

提交后,我希望呈现现有的自行车模型记录的显示页面,该页面与用户的选择相匹配(基于collection_selects中选定值的id)。

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

代码语言:javascript
复制
<%= 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 %>

共享/模型问题字段

代码语言:javascript
复制
<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 %>

共享/年份问题字段

代码语言:javascript
复制
<%= 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

代码语言:javascript
复制
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和年份来获取make_id。我对如何通过控制器获取记录感到困惑。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-24 06:27:59

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

bikes_controller

代码语言:javascript
复制
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

我没有正确地访问参数(它们是嵌套的)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19557368

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档