如何根据 RailsCasts 教程在 Rails 中进行简单搜索? [英] how to get simple search in rails based on RailsCasts tutorial?

查看:26
本文介绍了如何根据 RailsCasts 教程在 Rails 中进行简单搜索?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我按照 RailsCasts 教程使用自动完成功能进行了简单搜索,但它不起作用.:(

查看/供应商/索引:

<% form_tag vendor_path, :method =>'得到'做 %><%= text_field_with_auto_complete :供应商,:名称,{},{:方法=>:get, :class =>'文本框'} %><%= submit_tag "搜索", :name =>零%><%结束%>

<%= @searchvendor.id %><%=@searchterm %>

我包含了@searchterm 和@searchvendor.id 作为验证步骤.

所以这应该调用控制器=>供应商,动作=>索引:

def 索引@searchterm = params[:vendor][:name]@searchvendor = Vendor.search('checkpoint')

并为供应商/型号创建如下搜索方法:

 def self.search(search)如果搜索find(:all, :conditions => ['name LIKE ?', "%#{search}%"])别的找到所有)结尾

输出?

@searchterm 确实显示了输入的值,因为它在 URL 中显示为 vendor[name]=?

@searchvendor.id 是一个长字符串,@searchvendor.name 显示错误,名称不是方法.

我被难住了.帮助...请?

链接到教程:

http://railscasts.com/episodes/37-simple-search-form

解决方案

  1. 您不应该将@searchterm 传递到 Vendor.search 方法中吗?每次都通过检查点"可能不会成功.

  2. @searchvendor 将在您执行 find(:all) 时包含一个数组.您需要遍历数组并对每个项目执行 .name.

I followed the RailsCasts tutorial to do a simple search with autocomplete, but it doesn't work. :(

view/vendors/index:

<% form_tag vendors_path, :method => 'get' do %>
    <%= text_field_with_auto_complete   :vendor, 
                                        :name,
                                        {}, 
                                        {:method => :get, :class => 'textbox'} %>
    <%= submit_tag "Search", :name => nil %>
<% end %>
</div>
<%= @searchvendor.id %>
<%= @searchterm %>

I included @searchterm and @searchvendor.id as validation steps.

So this should call the controller => vendor, action=> index:

def index
    @searchterm = params[:vendor][:name]

    @searchvendor = Vendor.search('checkpoint')

And the search method is created as follows for the vendor/model:

  def self.search(search)
  if search
    find(:all, :conditions => ['name LIKE ?', "%#{search}%"])
  else
    find(:all)
  end

The output?

@searchterm does show the value that is inputed as that shows up in the URL as vendor[name]=?

@searchvendor.id is a long string and @searchvendor.name shows an error, name not a method.

I'm stumped. Help...please?

LINK to the Tutorial:

http://railscasts.com/episodes/37-simple-search-form

解决方案

  1. Should you not be passing in your @searchterm into your Vendor.search method? Passing 'checkpoint' each time probably isn't going to do the trick.

  2. @searchvendor is going to contain an Array as you are doing find(:all). You'll need to iterate over the array and do .name on each item.

这篇关于如何根据 RailsCasts 教程在 Rails 中进行简单搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆