Note that there are some explanatory texts on larger screens.

plurals
  1. PORuby on Rails: Show results on page, after search
    primarykey
    data
    text
    <p>I am trying to hide my search results until after the search has happened. At the moment when I click from the home page to the search page, the page shows the search boxes, but also shows all of the database in a table. I am wanting to just show the search options, and search button, then depending on the search, show the right table.</p> <p>Here is my search view:</p> <pre><code>&lt;h1&gt;Search&lt;/h1&gt; &lt;%= form_tag search_path, method: :get do %&gt; Client : &lt;%= select(@projects, :client, Project.all.map {|p| [p.client]}.uniq, :prompt =&gt; "-Any-", :selected =&gt; params[:client]) %&gt;&lt;/br&gt; Industry : &lt;%= select(@projects, :industry, Project.all.map {|p| [p.industry]}.uniq, :prompt =&gt; "-Any-", :selected =&gt; params[:industry]) %&gt;&lt;/br&gt; Role : &lt;%= select(@projects, :role, Project.all.map {|p| [p.role]}.uniq, :prompt =&gt; "-Any-", :selected =&gt; params[:role]) %&gt;&lt;/br&gt; Technologies : &lt;%= select(@projects, :tech, Project.all.map {|p| [p.tech]}.uniq, :prompt =&gt; "-Any-", :selected =&gt; params[:tech]) %&gt;&lt;/br&gt; Business Division : &lt;%= select(@projects, :business_div, Project.all.map {|p| [p.business_div]}.uniq, :prompt =&gt; "-Any-", :selected =&gt; params[:business_div]) %&gt;&lt;/br&gt; Project Owner : &lt;%= select(@projects, :project_owner, Project.all.map {|p| [p.project_owner]}.uniq, :prompt =&gt; "-Any-", :selected =&gt; params[:project_owner]) %&gt;&lt;/br&gt; Exception PM &lt;%= select(@projects, :exception_pm, Project.all.map {|p| [p.exception_pm]}.uniq, :prompt =&gt; "-Any-", :selected =&gt; params[:exception_pm]) %&gt;&lt;/br&gt; Start Date : &lt;%= text_field_tag("start_date") %&gt;&lt;/br&gt; End Date : &lt;%= text_field_tag("end_date") %&gt;&lt;/br&gt; Status : &lt;%= select(@projects, :status, Project.all.map {|p| [p.status]}.uniq, :prompt =&gt; "-Any-", :selected =&gt; params[:status]) %&gt;&lt;/br&gt; Keywords : &lt;%= text_field_tag :keywords, params[:keywords] %&gt;&lt;/br&gt; &lt;%= submit_tag "Search", name: nil %&gt; &lt;% end %&gt; &lt;% if @search %&gt; &lt;h3&gt;&lt;%=@project_search.total_entries%&gt; results&lt;/h3&gt; &lt;% end %&gt; &lt;% if @search %&gt; &lt;table class = "pretty"&gt; &lt;table border="1"&gt; &lt;tr&gt; &lt;th&gt;&lt;%= sortable "project_name", "Project name" %&gt; &lt;/th&gt; &lt;th&gt;&lt;%= sortable "client", "Client" %&gt;&lt;/th&gt; &lt;th&gt;Exception pm&lt;/th&gt; &lt;th&gt;Project owner&lt;/th&gt; &lt;th&gt;Tech&lt;/th&gt; &lt;th&gt;Role&lt;/th&gt; &lt;th&gt;Industry&lt;/th&gt; &lt;th&gt;Financials&lt;/th&gt; &lt;th&gt;Business div&lt;/th&gt; &lt;th&gt;Status&lt;/th&gt; &lt;th&gt;Start date&lt;/th&gt; &lt;th&gt;End date&lt;/th&gt; &lt;% if false %&gt; &lt;th&gt;Entry date&lt;/th&gt; &lt;th&gt;Edited date&lt;/th&gt; &lt;th&gt;Summary&lt;/th&gt; &lt;th&gt;Lessons learned&lt;/tStackh&gt; &lt;th&gt;Customer benifits&lt;/th&gt; &lt;th&gt;Keywords&lt;/th&gt; &lt;!th&gt;&lt;/th&gt; &lt;!th&gt;&lt;/th&gt; &lt;!th&gt;&lt;/th&gt; &lt;% end %&gt; &lt;/tr&gt; &lt;%# end %&gt; &lt;% @project_search.each do |t| %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= t.project_name %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= t.client %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= t.exception_pm %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= t.project_owner %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= t.tech %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= t.role %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= t.industry %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= t.financials %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= t.business_div %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= t.status %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= l t.start_date %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= l t.end_date %&gt;&lt;/td&gt; &lt;% if false %&gt; &lt;td&gt;&lt;%= t.entry_date %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= t.edited_date %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= t.summary %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= t.lessons_learned %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= t.customer_benifits %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= t.keywords %&gt;&lt;/td&gt; &lt;% end %&gt; &lt;!td&gt;&lt;%#= link_to 'Show', project %&gt;&lt;/td&gt; &lt;!td&gt;&lt;%#= link_to 'Edit', edit_project_path(project) %&gt;&lt;/td&gt; &lt;!td&gt;&lt;%#= link_to 'Destroy', project, method: :delete, data: { confirm: 'Are you sure?' } %&gt;&lt;/td&gt; &lt;/tr&gt; &lt;% end %&gt; &lt;/table&gt; Results per page: &lt;%= select_tag :per_page, options_for_select([10,20,50], params[:per_page].to_i), :onchange =&gt; "if(this.value){window.location='?per_page='+this.value;}" %&gt; &lt;br /&gt; &lt;%= hidden_field_tag :direction, params[:direction] %&gt; &lt;%= hidden_field_tag :sort, params[:sort] %&gt; &lt;%= hidden_field_tag :per_page, params[:per_page] %&gt; &lt;%= hidden_field_tag :page, params[:page] %&gt; &lt;%= will_paginate (@project_search) %&gt; &lt;%= button_to "Search Again?", search_path, :method =&gt; "get" %&gt; &lt;%# end %&gt; &lt;%= button_to "Home", projects_path, :method =&gt; "get" %&gt; </code></pre> <p>Here is the search action:</p> <pre><code>def search @search = params[:client], params[:industry], params[:role], params[:tech], params[:business_div], params[:project_owner], params[:exception_pm], params[:status], params[:start_date], params[:end_date], params[:keywords] @project_search = Project.search(@search) @project = Project.new(params[:project]) respond_to do |format| format.html # search.html.erb format.json { render :json =&gt; @project } end end </code></pre> <p>and here is my model.</p> <pre><code>class Project &lt; ActiveRecord::Base attr_accessible :business_div, :client, :customer_benifits, :edited_date, :end_date, :entry_date, :exception_pm, :financials, :industry, :keywords, :lessons_learned, :project_name, :project_owner, :role, :start_date, :status, :summary, :tech validates_presence_of :business_div, :client, :customer_benifits, :end_date, :exception_pm, :financials, :industry, :keywords, :lessons_learned, :project_name, :project_owner, :role, :start_date, :status, :summary, :tech def self.search(search_client, search_industry, search_role, search_tech, search_business_div, search_project_owner, search_exception_pm, search_status, search_start_date, search_end_date, search_keywords) return scoped unless search_client.present? || search_industry.present? || search_role.present? || search_tech.present? || search_business_div.present? || search_project_owner.present? || search_exception_pm.present? || search_status.present? || search_start_date.present? || search_end_date.present? || search_keywords.present? if search_start_date != "" search_start_date = Date.parse(search_start_date).strftime("%Y-%m-%d") end if search_end_date != "" search_end_date = Date.parse(search_end_date).strftime("%Y-%m-%d") end where(['client LIKE ? AND industry LIKE ? AND role LIKE ? AND tech LIKE ? AND business_div LIKE ? AND project_owner LIKE ? AND exception_pm LIKE ? AND status LIKE ? AND start_date LIKE ? AND end_date LIKE ? AND keywords LIKE ?', "%#{search_client}%", "%#{search_industry}%" , "%#{search_role}%" , "%#{search_tech}%" , "%#{search_business_div}%" , "%#{search_project_owner}%" , "%#{search_exception_pm}%" , "%#{search_status}%", "%#{search_start_date}%", "%#{search_end_date}%","%#{search_keywords}%"]) end def self.paginated_for_index(projects_per_page, current_page) paginate(:per_page =&gt; projects_per_page, :page =&gt; current_page) end end </code></pre> <p>Hopefully you can see what I have tried to do. I am getting the error <code>wrong number of arguments (1 for 11)</code> as I haven't split the params. But when I use <code>*@search</code> I get loads of errors.</p> <p>My original code in the controller looked like this before I went about trying to hide the table:</p> <p><code>@project_search = Project.search(params[:client], params[:industry], params[:role], params[:tech], params[:business_div], params[:project_owner], params[:exception_pm], params[:status], params[:start_date], params[:end_date], params[:keywords]).order(sort_column + ' ' + sort_direction).paginated_for_index(per_page, page</code>)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload