简体   繁体   English

Ruby on Rails搜索表单

[英]ruby on rails search form

I'm new to RoR and I've managed to make a basic search form but keep getting errors when trying to expand the search tags (name).. I have a model with various data (location, website, email, telephone) and was wondering how I can add these to my current search code. 我是RoR的新手,我设法制作了一个基本的搜索表单,但是在尝试扩展搜索标签(名称)时一直出现错误。.我有一个包含各种数据(位置,网站,电子邮件,电话)的模型,想知道如何将它们添加到当前的搜索代码中。

/models/ciir.rb

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

static_pages_controller.rb

def home
  @ciirs = Ciir.search(params[:search])
end

/home.html.erb

<%= form_tag ciirs_path, :method => 'get' do %>
  <p>
    <%= text_field_tag :search, params[:search] %>
    <%= submit_tag " Search Database Records ", :name => nil %>
  </p>
<% end %>

When clicking the submit button (no search terms) the url is: 单击提交按钮(无搜索词)时,URL为:

ciirs?utf8=✓&search=

but when modifying the name condition to something like 'website' the url changes to 但是在将名称条件修改为“网站”之类时,URL更改为

ciirs?utf8=✓&search=&commit=+Search+Database+Records+ –

Since you mentioned you are new to RoR, I must share the way I learned RoR was reading, using and analyzing one issue at a time. 既然您提到您是RoR的新手,那么我必须分享我所学到的RoR每次阅读,使用和分析一个问题的方式。 I would suggest you to take a look at following points one at a time and try & learn how RoR treats them and how these fit your question: 我建议您一次查看以下几点,并尝试了解RoR如何对待它们以及这些如何满足您的问题:

  1. How form_tag works? form_tag如何工作?

  2. How text_field_tag works? text_field_tag如何工作?

  3. Once you have understood form_tag , difference between text_field_tag and f.text_field ? 了解了form_tagtext_field_tagf.text_field之间的f.text_field什么?

  4. How params objects are created, and it uses names of form controls? 如何创建params对象,并使用表单控件的名称?

  5. How and when to use GET and/or POST form methods? 如何以及何时使用GET和/或POST表单方法? Inadvertently, what are different types of method and when to use them? 不经意间,有什么不同类型的方法以及何时使用它们?

  6. How URL are used in the form_tag and what components are they made of? URLform_tag如何使用,它们由什么组成?

  7. Sprinkle a bit of knowledge of Ruby language by learning between Array s and Hash es? 通过在ArrayHash之间学习来对Ruby语言有所了解? In fact, learn Ruby as much as you can. 实际上,尽可能多地学习Ruby

Answering your question, 回答你的问题,

/home.html.erb

<%= form_tag "/static_pages/home", :method => 'post' do %>
  <p>
    <%= text_field_tag "search[name]", params.has_key?("search") && params[:search].has_key?("name") ? params[:search][:name] : "" %>
    <%= submit_tag " Search Database Records " %>
  </p>
<% end %>

/models/ciir.rb

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

So I modified your form, and told RoR about search params containing data for name . 因此,我修改了您的表格,并告诉RoR有关包含name数据的search params

params is a Hash (which is a key-value pair ) having key named search , which further is a Hash having key named name . params是具有名为searchkey-value pairHash (这是一个key-value pair ),进一步是具有名为name键的Hash

The same principle is followed in the model code. 模型代码遵循相同的原理。 We passed the Hash of key search to the function and in there, used the value of key named name . 我们将键searchHash传递给该函数,并在其中使用名为name的键的值。

I also updated the url in form_tag , to point it to home action of your controller. 我还更新了form_tag的url,以将其指向控制器的首页操作。 Assuming that you have added it to your routes.rb file, it usually follows the pattern controller_name/action_name or the function name action_name_controller_name_path or action_name_controller_name_url . 假设已将其添加到routes.rb文件中,则通常遵循模式controller_name/action_name或函数名称action_name_controller_name_pathaction_name_controller_name_url Run rake routes command at your root directory to list out all paths in your application. 在根目录中运行rake routes命令以列出应用程序中的所有路径。

Also note, I used POST method instead of original GET . 还要注意,我使用POST方法而不是原始的GET You may wish to use GET here, so please change it back. 您可能希望在此处使用GET ,因此请改回来。

I hope this works. 我希望这行得通。

I found no error in your code. 我发现您的代码没有错误。 the url changed to ciirs?utf8=✓&search=&commit=+Search+Database+Records+ is normal. url更改为ciirs?utf8=✓&search=&commit=+Search+Database+Records+是正常的。 submit_tag generates a button named "commit" defaultly, it will be parsed in the params. submit_tag默认情况下会生成一个名为“ commit”的button ,它将在参数中进行解析。 I see you add :name => nil , it will fix the problem, the other part of your code needn't to be modified. 我看到您添加了:name => nil,它将解决问题,无需修改代码的另一部分。 I copied your code and tested it, it ran smoothly. 我复制了您的代码并对其进行了测试,它运行平稳。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM