{"id":616,"date":"2011-11-28T18:44:58","date_gmt":"2011-11-28T18:44:58","guid":{"rendered":"http:\/\/41j.com\/blog\/?p=616"},"modified":"2011-11-28T16:51:06","modified_gmt":"2011-11-28T16:51:06","slug":"adding-search-to-a-rails-app","status":"publish","type":"post","link":"https:\/\/41j.com\/blog\/2011\/11\/adding-search-to-a-rails-app\/","title":{"rendered":"Adding search to a Rails app."},"content":{"rendered":"<p>I wanted to add search to my Rails application. Here&#8217;s what I did.<\/p>\n<p>1. Search code to the controller, this controller was already built using a scaffold around a Table called &#8220;Words&#8221;:<\/p>\n<p>controllers\/words_controller.rb:<\/p>\n<pre class=\"brush: ruby; title: ; notranslate\" title=\"\">\r\n  def search\r\n    @words = Word.where(&quot;word = ?&quot;,params&#x5B;:id]) #all #find(params&#x5B;:id])\r\n\r\n    respond_to do |format|\r\n      format.html # search.html.erb\r\n      format.json { render json: @words }\r\n    end\r\n  end\r\n<\/pre>\n<p>I added a view based around the index.html.erb already present. It&#8217;s basically identical:<\/p>\n<p>app\/views\/words\/search.html.erb<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;h1&gt;Search results&lt;\/h1&gt;\r\n\r\n&lt;table&gt;\r\n  &lt;tr&gt;\r\n    &lt;th&gt;Word&lt;\/th&gt;\r\n    &lt;th&gt;Definition&lt;\/th&gt;\r\n    &lt;th&gt;Tags&lt;\/th&gt;\r\n    &lt;th&gt;&lt;\/th&gt;\r\n    &lt;th&gt;&lt;\/th&gt;\r\n    &lt;th&gt;&lt;\/th&gt;\r\n  &lt;\/tr&gt;\r\n\r\n&lt;% @words.each do |word| %&gt;\r\n  &lt;tr&gt;\r\n    &lt;td&gt;&lt;%= word.word %&gt;&lt;\/td&gt;\r\n    &lt;td&gt;&lt;%= word.tags %&gt;&lt;\/td&gt;\r\n    &lt;td&gt;&lt;%= word.definition %&gt;&lt;\/td&gt;\r\n    &lt;td&gt;&lt;%= link_to 'Show', word %&gt;&lt;\/td&gt;\r\n    &lt;td&gt;&lt;%= link_to 'Edit', edit_word_path(word) %&gt;&lt;\/td&gt;\r\n    &lt;td&gt;&lt;%= link_to 'Destroy', word, confirm: 'Are you sure?', method: :delete %&gt;&lt;\/td&gt;\r\n  &lt;\/tr&gt;\r\n&lt;% end %&gt;\r\n&lt;\/table&gt;\r\n\r\n&lt;br \/&gt;\r\n\r\n&lt;%= link_to 'New Word', new_word_path %&gt;\r\n\r\n&lt;% if session&#x5B;:user_id] %&gt;\r\n&lt;%= button_to 'Logout', logout_path, method: :delete %&gt;\r\n&lt;% end %&gt;\r\n&lt;\/div&gt;\r\n&lt;div id=&quot;main&quot; &gt;\r\n&lt;%= yield %&gt;\r\n<\/pre>\n<p>I added the following rule to config\/routes.rb:<\/p>\n<pre class=\"brush: ruby; title: ; notranslate\" title=\"\">\r\nmatch ':controller(\/:action(\/:id))'\r\n<\/pre>\n<p>Which if I&#8217;ve understood it correctly sends any http:\/\/blah\/anycontroller\/action\/something to the anycontroller&#8217;s action method using something as a parameter (kind of).<\/p>\n<p>I can then do searches such as:<\/p>\n<p>http:\/\/localhost:3000\/words\/search\/wordtosearchfor<\/p>\n<h2>Notes<\/h2>\n<p>Will need to add a html form to perform the search (somehow).<br \/>\nClearly not great if you&#8217;re code is returning a lot of search results.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I wanted to add search to my Rails application. Here&#8217;s what I did. 1. Search code to the controller, this controller was already built using a scaffold around a Table called &#8220;Words&#8221;: controllers\/words_controller.rb: def search @words = Word.where(&quot;word = ?&quot;,params&#x5B;:id]) #all #find(params&#x5B;:id]) respond_to do |format| format.html # search.html.erb format.json { render json: @words } end [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[],"class_list":["post-616","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1RRoU-9W","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/616","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/comments?post=616"}],"version-history":[{"count":1,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/616\/revisions"}],"predecessor-version":[{"id":617,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/616\/revisions\/617"}],"wp:attachment":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/media?parent=616"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/categories?post=616"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/tags?post=616"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}