{"id":620,"date":"2011-11-28T22:52:00","date_gmt":"2011-11-28T22:52:00","guid":{"rendered":"http:\/\/41j.com\/blog\/?p=620"},"modified":"2011-11-28T22:52:00","modified_gmt":"2011-11-28T22:52:00","slug":"ruby-on-the-rails-search-functionality-2nd-attempt","status":"publish","type":"post","link":"https:\/\/41j.com\/blog\/2011\/11\/ruby-on-the-rails-search-functionality-2nd-attempt\/","title":{"rendered":"Ruby on the Rails Search Functionality 2nd attempt"},"content":{"rendered":"<p>My first attempt is <a href=\"http:\/\/41j.com\/blog\/2011\/11\/adding-search-to-a-rails-app\/\">here<\/a>. However that lacked a search box. It was neat in that you could reference the search directly as http:\/\/blah\/blah\/searchstring. However that&#8217;s not really what we want, as we want to use search forms and such&#8230;.<\/p>\n<p>So attempt 2.<\/p>\n<p>First add routes for 2 net methods to routes.rb:<\/p>\n<pre class=\"brush: ruby; title: ; notranslate\" title=\"\">\r\n  resources :words do\r\n    collection do\r\n      get 'dosearch'\r\n      get 'search'\r\n    end\r\n  end\r\n<\/pre>\n<p>I then added 2 new methods to the appropriate controller:<\/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\r\n  def dosearch\r\n    @word = &quot;&quot;\r\n    respond_to do |format|\r\n      format.html\r\n      format.json { render json: word }\r\n    end\r\n  end\r\n<\/pre>\n<p>The first method is as before, and performs the search. The second method really just returns the search html. Perhaps I shouldn&#8217;t be using a method for this at all? Just some static html?<\/p>\n<p>Then I added a dosearch.html.erb which will render the search form:<\/p>\n<pre class=\"brush: ruby; title: ; notranslate\" title=\"\">\r\n&lt;h1&gt;Search for word&lt;\/h1&gt;\r\n\r\n&lt;%= render 'searchform' %&gt;\r\n\r\n&lt;%= link_to 'Back', words_path %&gt;\r\n<\/pre>\n<p>And a _searchform.html.erb which \/actually\/ renders the form:<\/p>\n<pre class=\"brush: ruby; title: ; notranslate\" title=\"\">\r\n%= form_tag(&quot;\/words\/search&quot;, :method =&gt; &quot;get&quot;) do %&gt;\r\n  &lt;%= label_tag(:q, &quot;Search for:&quot;) %&gt;\r\n  &lt;%= text_field_tag(:id) %&gt;\r\n  &lt;%= submit_tag(&quot;Search&quot;) %&gt;\r\n&lt;% end %&gt;\r\n<\/pre>\n<p>Next I added a search.html.erb for the search results:<\/p>\n<pre class=\"brush: ruby; 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>Which is pretty much a standard index.html.erb to return a list.<\/p>\n<p>That is all.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>My first attempt is here. However that lacked a search box. It was neat in that you could reference the search directly as http:\/\/blah\/blah\/searchstring. However that&#8217;s not really what we want, as we want to use search forms and such&#8230;. So attempt 2. First add routes for 2 net methods to routes.rb: resources :words do [&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-620","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1RRoU-a0","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/620","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=620"}],"version-history":[{"count":1,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/620\/revisions"}],"predecessor-version":[{"id":621,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/620\/revisions\/621"}],"wp:attachment":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/media?parent=620"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/categories?post=620"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/tags?post=620"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}