[英]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如何对待它们以及这些如何满足您的问题:
How form_tag
works? form_tag
如何工作?
How text_field_tag
works? text_field_tag
如何工作?
Once you have understood form_tag
, difference between text_field_tag
and f.text_field
? 了解了
form_tag
, text_field_tag
和f.text_field
之间的f.text_field
什么?
How params
objects are created, and it uses names of form controls? 如何创建
params
对象,并使用表单控件的名称?
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? 不经意间,有什么不同类型的方法以及何时使用它们?
How URL
are used in the form_tag
and what components are they made of? URL
在form_tag
如何使用,它们由什么组成?
Sprinkle a bit of knowledge of Ruby
language by learning between Array
s and Hash
es? 通过在
Array
和Hash
之间学习来对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
是具有名为search
的key-value pair
的Hash
(这是一个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
. 我们将键
search
的Hash
传递给该函数,并在其中使用名为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_path
或action_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.