Hello all, This one has kept me up at night. I''m kind of new to Ruby on Rails (5 weeks and counting). I have two tables. One table is created by the input of a .csv file. Imports - imports_id - imports_title - description - manager The other table is a "projects" table. Projects - projects_id - project_title - description - manager Displaying the Imports table I can hover over a row and highlight the individual rows. What I''m trying to accomplish is when I click on a highlighted row, I wish to populate an empty table to the right of the Import table. The table on the right would be results from a search of the "projects" table. Currently, imports_id and projects_id are not the same format, nor are the "titles". I need to search through the "projects" table for similar titles and/or ''id''s. I have the two tables on a page and can populate the "Imports" table. I''ve been doing a lot of reading and it looks like I would use "remote_function" to call a function via ajax. I know this is a lot to ask but I need help. Thank you for any and all advice. JohnM -- Posted via http://www.ruby-forum.com/.
Aldric Giacomoni
2009-Sep-11  19:40 UTC
Re: Two tables, clickable row one table to fill other table
Hang on to your shorts - I did something quite similar.
It''s very simple, I have two models, each are a table in a different 
database, but when you get to Rails level, you don''t care -
it''s just
two models.
On the main page, I have two divs: patient and searchresults
Above the divs, I have a :
form_remote_tag :update => "patient", :url => { :action =>
"search" }
I use this to get a string (hint : text_field_tag is your friend)
My controller has, for the action "search", the following:
  def search
    name = params[:search]
    results = GMPatient.filter_by_name(name, params[:page])
    render :partial => ''shared/paginated_gm_patients'',
      :locals => {:patients => results}
  end
What is "filter_by_name", you ask? Good question! I use the 
will-paginate gem. My model has the following:
  def self.filter_by_name(search, page)
    paginate :per_page => 20, :page => page,
    :conditions => [''displayed_name like ?'',
"%#{search}%"],
    :order => ''last ASC, first ASC''
  end
The ''%'' sign is my wildcard. So, now that I have the results,
how do I
display them?
It''s your standard table, but here''s the twist: I add a cell
after the
data I display, and here''s what''s in the cell:
<td><%= link_to_remote "Find matches", :update =>
"matches", :url => {
:controller => "comparison", :action =>
"find_matches", :id =>
patient.id} %>
So what''s my "find_matches" action? BACK TO THE CONTROLLER! I
feel like
I''m in the movie Clue!
  def find_matches
    gmpatient = GMPatient.find_by_id params[:id]
    results = SybasePatient.find :all,
      :conditions => ["birth_date = ? OR identifier = ? OR pat_name like
?",
      gmpatient.dob, gmpatient.ssn, 
"%#{gmpatient.last}%#{gmpatient.first}%"
      ]
    render :partial => ''shared/pacs_patients'',
      :locals => { :patients => results }
  end
Lastly then, what is this partial? It''s another simple table!
I hope this helps.
-- 
Posted via http://www.ruby-forum.com/.
Aldric Giacomoni
2009-Sep-11  19:41 UTC
Re: Two tables, clickable row one table to fill other table
Hrm. At the end, where I have :update => "matches" , for consistency''s sake, please read :update => "searchresults". -- Posted via http://www.ruby-forum.com/.
John Mcleod
2009-Sep-11  19:50 UTC
Re: Two tables, clickable row one table to fill other table
Whoa! There''s a lot to chew on there. First off, thank you for the reply. Let me chew, chew on this over the weekend and I''ll let you know my progress on Monday. I''ve used wil_paginate before so that helps. Thanks again. John Aldric Giacomoni wrote:> Hang on to your shorts - I did something quite similar. > It''s very simple, I have two models, each are a table in a different > database, but when you get to Rails level, you don''t care - it''s just > two models. > > On the main page, I have two divs: patient and searchresults > Above the divs, I have a : > form_remote_tag :update => "patient", :url => { :action => "search" } > I use this to get a string (hint : text_field_tag is your friend) > > My controller has, for the action "search", the following: > > def search > name = params[:search] > results = GMPatient.filter_by_name(name, params[:page]) > > render :partial => ''shared/paginated_gm_patients'', > :locals => {:patients => results} > end > > What is "filter_by_name", you ask? Good question! I use the > will-paginate gem. My model has the following: > > def self.filter_by_name(search, page) > paginate :per_page => 20, :page => page, > :conditions => [''displayed_name like ?'', "%#{search}%"], > :order => ''last ASC, first ASC'' > end > > The ''%'' sign is my wildcard. So, now that I have the results, how do I > display them? > > It''s your standard table, but here''s the twist: I add a cell after the > data I display, and here''s what''s in the cell: > <td><%= link_to_remote "Find matches", :update => "matches", :url => { > :controller => "comparison", :action => "find_matches", :id =>... -- Posted via http://www.ruby-forum.com/.
John Mcleod
2009-Sep-16  18:27 UTC
Re: Two tables, clickable row one table to fill other table
Aldric,
Well, I finally had enough time to start work on it.
Let''s start at the beginning.
I have to divs (imports and searchResults)
Now maybe I''ve changed this around a little but in the
"imports" table I
added a cell after each row with...
<td><%= link_to_remote "Find Matches", :update =>
"projects", :url =>
{:controller => ''projects'', :action =>
''search'', :id => import.import_id
}  %></td>
This triggers the search in the "Projects Controller" for which I
search
for similar unique import IDs.
-ProjectsController-
def search
  import_id = params[:search]
  results = Project.search_by_irb_pi(import_id, params[:page])
  render :partial => ''imports/results'', :locals =>
{:projects =>
results}
end
In the Model, I have this...
-Model-
def self.search_by_import_pi(search, page)
  paginate( :per_page => 10, :page => page,
            :conditions => [''import_id LIKE ?'',
"%#{search}%"],
            :order => ''import_id ASC, pi_full_name ASC'')
end
The Ajax call works fine, but the query sent to the "projects" table 
looks like this...
 [0;1mSELECT * FROM "projects" WHERE (import_id LIKE
''%%'') ORDER BY
irb_id ASC, pi_full_name ASC LIMIT 10 OFFSET 0 [0m
This will return nothing of course.
Another problem is trying to display the results in the table in the 
"searchResults" div on the same page.
Thanks again
John
John Mcleod wrote:> Whoa!  There''s a lot to chew on there.
> First off, thank you for the reply.
> Let me chew, chew on this over the weekend and I''ll let you know
my
> progress on Monday.
> I''ve used wil_paginate before so that helps.
> Thanks again.
> John
> 
> Aldric Giacomoni wrote:
>> Hang on to your shorts - I did something quite similar.
>> It''s very simple, I have two models, each are a table in a
different
>> database, but when you get to Rails level, you don''t care -
it''s just
>> two models.
>> 
>> On the main page, I have two divs: patient and searchresults
>> Above the divs, I have a :
>> form_remote_tag :update => "patient", :url => { :action
=> "search" }
>> I use this to get a string (hint : text_field_tag is your friend)
>> 
>> My controller has, for the action "search", the following:
>> 
>>   def search
>>     name = params[:search]
>>     results = GMPatient.filter_by_name(name, params[:page])
>> 
>>     render :partial =>
''shared/paginated_gm_patients'',
>>       :locals => {:patients => results}
>>   end
>> 
>> What is "filter_by_name", you ask? Good question! I use the 
>> will-paginate gem. My model has the following:
>> 
>>   def self.filter_by_name(search, page)
>>     paginate :per_page => 20, :page => page,
>>     :conditions => [''displayed_name like ?'',
"%#{search}%"],
>>     :order => ''last ASC, first ASC''
>>   end
>> 
>> The ''%'' sign is my wildcard. So, now that I have the
results, how do I
>> display them?
>> 
>> It''s your standard table, but here''s the twist: I add
a cell after the
>> data I display, and here''s what''s in the cell:
>> <td><%= link_to_remote "Find matches", :update =>
"matches", :url => {
>> :controller => "comparison", :action =>
"find_matches", :id =>
> ...
-- 
Posted via http://www.ruby-forum.com/.
Aldric Giacomoni
2009-Sep-17  12:31 UTC
Re: Two tables, clickable row one table to fill other table
John Mcleod wrote: As I am reading your message, I keep on wondering where you get your params[:search] from, and it turns out you''re not telling me! I made a mistake when I wrote my message to you - I didn''t specify it, either. My text_field is called ''search'' and that''s why it works for me. You''re sending the id via :id, so .. def search import_id = params[:id] ... end Should work a little better. also, you may want to do: <%= link_to_remote "Find Matches", :update => "searchResults" .... And lastly, "search_by_irb_pi" and "search_by_import_pi" don''t seem to coincide :-)> -ProjectsController- > def search > import_id = params[:search] > results = Project.search_by_irb_pi(import_id, params[:page]) > render :partial => ''imports/results'', :locals => {:projects => > results} > end > > In the Model, I have this... > > -Model- > def self.search_by_import_pi(search, page) > paginate( :per_page => 10, :page => page, > :conditions => [''import_id LIKE ?'', "%#{search}%"], > :order => ''import_id ASC, pi_full_name ASC'') > endTry it that way and let me know. -- Posted via http://www.ruby-forum.com/.
John Mcleod
2009-Sep-22  13:49 UTC
Re: Two tables, clickable row one table to fill other table
Aldric,
Sorry, I just did some work on the project and it still doesn''t update 
the searchResults table.
So, here''s my index view...
- index.html.erb -
<h1>Listing imports</h1>
<div id="import_container">
  <% form_remote_tag :update => ''project'', :url => {
:action => ''search''
} do %>
    <div class="csv">
      <table>
        <tr>
          <th colspan="5">IRB File</th>
        </tr>
        <tr>
          <th>IRB ID</th>
          <th>Title</th>
          <th>Pi full name</th>
          <th colspan="2">Action</th>
        </tr>
        <% @imports.each do |import| %>
          <tr>
              <td><%=h import.irb_id %></td>
              <td><%=h import.title %></td>
              <td><%=h import.pi_full_name %></td>
              <td style="text-align:center;"><%= link_to
''view'', import,
:rel => ''facebox'' %></td>
              <td><%= link_to_remote "Find Matches", :update
=>
"search_results", :url => {:controller =>
''projects'', :action =>
''search'', :id => import.irb_id }  %></td>
          </tr>
        <% end %>
      </table>
    </div>
  <% end %>
  <div class="search_results">
    <table>
      <tr>
        <th colspan="5">Search Results</th>
      </tr>
      <tr>
        <th>Project ID</th>
        <th>IRB ID</th>
        <th>Title</th>
        <th>Pi full name</th>
        <th>Action</th>
      </tr>
      <% @projects.each do |project| %>
      <tr>
          <td><%=h project.irb_id %></td>
          <td><%=h project.project_id %></td>
          <td><%=h project.title %></td>
          <td><%=h project.pi_full_name %></td>
          <td style="text-align:center;"><%= link_to
''view'', project,
:rel => ''facebox'' %></td>
        </tr>
      <% end %>
  </table>
  </div>
Thanks again.
John
Aldric Giacomoni wrote:> John Mcleod wrote:
> 
> As I am reading your message, I keep on wondering where you get your 
> params[:search] from, and it turns out you''re not telling me!
> I made a mistake when I wrote my message to you - I didn''t specify
it,
> either. My text_field is called ''search'' and
that''s why it works for me.
> You''re sending the id via :id, so ..
> 
> def search
>   import_id = params[:id]
>   ...
> end
> 
> Should work a little better.
> 
> also, you may want to do:
> <%= link_to_remote "Find Matches", :update =>
"searchResults" ....
> 
> And lastly, "search_by_irb_pi" and
"search_by_import_pi" don''t seem to
> coincide :-)
> 
>> -ProjectsController-
>> def search
>>   import_id = params[:search]
>>   results = Project.search_by_irb_pi(import_id, params[:page])
>>   render :partial => ''imports/results'', :locals
=> {:projects =>
>> results}
>> end
>> 
>> In the Model, I have this...
>> 
>> -Model-
>> def self.search_by_import_pi(search, page)
>>   paginate( :per_page => 10, :page => page,
>>             :conditions => [''import_id LIKE ?'',
"%#{search}%"],
>>             :order => ''import_id ASC, pi_full_name
ASC'')
>> end
> 
> Try it that way and let me know.
-- 
Posted via http://www.ruby-forum.com/.
Aldric Giacomoni
2009-Sep-22  15:33 UTC
Re: Two tables, clickable row one table to fill other table
John Mcleod wrote:> Aldric, > Sorry, I just did some work on the project and it still doesn''t update > the searchResults table. >> <td><%= link_to_remote "Find Matches", :update => > "search_results", :url => {:controller => ''projects'', :action => > ''search'', :id => import.irb_id } %></td> > > > <div class="search_results"> > <table> > <tr> > <th colspan="5">Search Results</th> > </tr> > <tr> > <th>Project ID</th> > <th>IRB ID</th> > <th>Title</th> > <th>Pi full name</th> > <th>Action</th> > </tr> > > <% @projects.each do |project| %> > <tr> > <td><%=h project.irb_id %></td> > <td><%=h project.project_id %></td> > <td><%=h project.title %></td> > <td><%=h project.pi_full_name %></td> > <td style="text-align:center;"><%= link_to ''view'', project, > :rel => ''facebox'' %></td> > </tr> > <% end %> > </table> > </div>What I would do is put that table in a partial, and then have the action ''search'' render said partial, possibly with :locals => { :projects => @projects } so you can use a local variable ''projects'' to create your table. I do not know if plain updating the way you want to do it works (I don''t mean that I don''t think it works - I literally mean I have no idea), so that''s how I''d set it up. -- Posted via http://www.ruby-forum.com/.
John Mcleod
2009-Sep-24  17:30 UTC
Re: Two tables, clickable row one table to fill other table
Aldric,
I did what you suggested and still haven''t displayed anything.  I am 
however getting results per Firebug, but no display.
Here''s my model and controller again.
- Model -
def self.search_by_irb_pi(search, page)
     paginate( :per_page => 10, :page => page,
             :conditions => [''irb_id LIKE ?'',
"%#{search}%"],
             :order => ''irb_id ASC'')
end
- Controller -
def search
    irb_id = params[:id]
    results = Project.search_by_irb_pi(irb_id, params[:page])
    render :partial => ''imports/results'', :locals =>
{:projects =>
@projects}
end
Thank you again for all your help
John
Aldric Giacomoni wrote:> John Mcleod wrote:
>> Aldric,
>> Sorry, I just did some work on the project and it still
doesn''t update
>> the searchResults table.
>> 
> 
>>               <td><%= link_to_remote "Find Matches",
:update =>
>> "search_results", :url => {:controller =>
''projects'', :action =>
>> ''search'', :id => import.irb_id }  %></td>
>> 
>> 
>>   <div class="search_results">
>>     <table>
>>       <tr>
>>         <th colspan="5">Search Results</th>
>>       </tr>
>>       <tr>
>>         <th>Project ID</th>
>>         <th>IRB ID</th>
>>         <th>Title</th>
>>         <th>Pi full name</th>
>>         <th>Action</th>
>>       </tr>
>> 
>>       <% @projects.each do |project| %>
>>       <tr>
>>           <td><%=h project.irb_id %></td>
>>           <td><%=h project.project_id %></td>
>>           <td><%=h project.title %></td>
>>           <td><%=h project.pi_full_name %></td>
>>           <td style="text-align:center;"><%= link_to
''view'', project,
>> :rel => ''facebox'' %></td>
>>         </tr>
>>       <% end %>
>>   </table>
>>   </div>
> 
> What I would do is put that table in a partial, and then have the action 
> ''search'' render said partial, possibly with :locals =>
{ :projects =>
> @projects } so you can use a local variable ''projects'' to
create your
> table.
> I do not know if plain updating the way you want to do it works (I
don''t
> mean that I don''t think it works - I literally mean I have no
idea), so
> that''s how I''d set it up.
-- 
Posted via http://www.ruby-forum.com/.
John Mcleod
2009-Sep-24  17:50 UTC
Re: Two tables, clickable row one table to fill other table
You probably want to see my partial
- results.html.erb -
<table>
      <tr>
        <th colspan="5">Project</th>
      </tr>
      <tr>
        <th>Project ID</th>
        <th>IRB ID</th>
        <th>Title</th>
        <th>Pi full name</th>
        <th>Action</th>
      </tr>
      <% @projects.each do |project| %>
      <tr>
          <td><%=h project.irb_id %></td>
          <td><%=h project.project_id %></td>
          <td><%=h project.title %></td>
          <td><%=h project.pi_full_name %></td>
          <td style="text-align:center;"><%= link_to
''view'', project,
:rel => ''facebox'' %></td>
        </tr>
      <% end %>
  </table>
John
John Mcleod wrote:> Aldric,
> I did what you suggested and still haven''t displayed anything.  I
am
> however getting results per Firebug, but no display.
> Here''s my model and controller again.
> 
> - Model -
> def self.search_by_irb_pi(search, page)
> 
>      paginate( :per_page => 10, :page => page,
>              :conditions => [''irb_id LIKE ?'',
"%#{search}%"],
>              :order => ''irb_id ASC'')
> end
> 
> - Controller -
> def search
>     irb_id = params[:id]
>     results = Project.search_by_irb_pi(irb_id, params[:page])
>     render :partial => ''imports/results'', :locals
=> {:projects =>
> @projects}
> end
> 
> Thank you again for all your help
> 
> John
> 
> Aldric Giacomoni wrote:
>> John Mcleod wrote:
>>> Aldric,
>>> Sorry, I just did some work on the project and it still
doesn''t update
>>> the searchResults table.
>>> 
>> 
>>>               <td><%= link_to_remote "Find
Matches", :update =>
>>> "search_results", :url => {:controller =>
''projects'', :action =>
>>> ''search'', :id => import.irb_id } 
%></td>
>>> 
>>> 
>>>   <div class="search_results">
>>>     <table>
>>>       <tr>
>>>         <th colspan="5">Search Results</th>
>>>       </tr>
>>>       <tr>
>>>         <th>Project ID</th>
>>>         <th>IRB ID</th>
>>>         <th>Title</th>
>>>         <th>Pi full name</th>
>>>         <th>Action</th>
>>>       </tr>
>>> 
>>>       <% @projects.each do |project| %>
>>>       <tr>
>>>           <td><%=h project.irb_id %></td>
>>>           <td><%=h project.project_id %></td>
>>>           <td><%=h project.title %></td>
>>>           <td><%=h project.pi_full_name %></td>
>>>           <td style="text-align:center;"><%=
link_to ''view'', project,
>>> :rel => ''facebox'' %></td>
>>>         </tr>
>>>       <% end %>
>>>   </table>
>>>   </div>
>> 
>> What I would do is put that table in a partial, and then have the
action
>> ''search'' render said partial, possibly with :locals
=> { :projects =>
>> @projects } so you can use a local variable
''projects'' to create your
>> table.
>> I do not know if plain updating the way you want to do it works (I
don''t
>> mean that I don''t think it works - I literally mean I have no
idea), so
>> that''s how I''d set it up.
-- 
Posted via http://www.ruby-forum.com/.
Aldric Giacomoni
2009-Sep-24  19:05 UTC
Re: Two tables, clickable row one table to fill other table
I''m curious:
- Controller -
irb_id = params[:id]
    results = Project.search_by_irb_pi(irb_id, params[:page])
    render :partial => ''imports/results'', :locals =>
{:projects =>
@projects}
This code quite clearly gets results, and then sends the variable 
@projects out to the partial. Why? I''d expect it to send :projects
=>
results
John Mcleod wrote:> 
> - results.html.erb -
> 
> <table>
>       <tr>
>         <th colspan="5">Project</th>
>       </tr>
>       <tr>
>         <th>Project ID</th>
>         <th>IRB ID</th>
>         <th>Title</th>
>         <th>Pi full name</th>
>         <th>Action</th>
>       </tr>
>       <% @projects.each do |project| %>
If you follow what I said above, then this should be
''projects.each''
(the :projects becomes a variable named ''projects'').
Try that.
-- 
Posted via http://www.ruby-forum.com/.
John Mcleod
2009-Sep-28  15:15 UTC
Re: Two tables, clickable row one table to fill other table
Aldric, That did it! I have results and they display in the searchResults div. I changed.. :projects => @projects to :projects => results I can''t thank you enough for helping me on this. Next, I''ll be attaching an ''onclick'' function to the row of searchResults then link the id to a button(s) to either save, delete the associated row. But that''s my next step. You''ve helped enough. If I get stuck again, which I bet I will, I''ll post again. Thank you again. John Aldric Giacomoni wrote:> I''m curious: > - Controller - > irb_id = params[:id] > results = Project.search_by_irb_pi(irb_id, params[:page]) > render :partial => ''imports/results'', :locals => {:projects => > @projects} > > This code quite clearly gets results, and then sends the variable > @projects out to the partial. Why? I''d expect it to send :projects => > results > > > John Mcleod wrote: >> >> - results.html.erb - >> >> <table> >> <tr> >> <th colspan="5">Project</th> >> </tr> >> <tr> >> <th>Project ID</th> >> <th>IRB ID</th> >> <th>Title</th> >> <th>Pi full name</th> >> <th>Action</th> >> </tr> >> <% @projects.each do |project| %> > > If you follow what I said above, then this should be ''projects.each'' > (the :projects becomes a variable named ''projects''). > > Try that.-- Posted via http://www.ruby-forum.com/.
Aldric Giacomoni
2009-Sep-28  15:19 UTC
Re: Two tables, clickable row one table to fill other table
John Mcleod wrote:> Aldric, > That did it! > I have results and they display in the searchResults div. > I changed.. :projects => @projects to :projects => results > > I can''t thank you enough for helping me on this.My pleasure! People help me.. I help people. The world goes round, albeit slightly lopsided.> > Next, I''ll be attaching an ''onclick'' function to the row of > searchResults then link the id to a button(s) to either save, delete the > associated row. > But that''s my next step. You''ve helped enough. > If I get stuck again, which I bet I will, I''ll post again. > > Thank you again. > > JohnHmm.. Why not just do it the way the scaffolds set it up? :) -- Posted via http://www.ruby-forum.com/.
John Mcleod
2009-Sep-28  15:24 UTC
Re: Two tables, clickable row one table to fill other table
> My pleasure! People help me.. I help people. The world goes round, > albeit slightly lopsided.quite a bit lopsided.> Hmm.. Why not just do it the way the scaffolds set it up? :)Not a bad idea. I''ll head in that direction and see what transpires. Aldric Giacomoni wrote:> John Mcleod wrote: >> Aldric, >> That did it! >> I have results and they display in the searchResults div. >> I changed.. :projects => @projects to :projects => results >> >> I can''t thank you enough for helping me on this. > > My pleasure! People help me.. I help people. The world goes round, > albeit slightly lopsided. > >> >> Next, I''ll be attaching an ''onclick'' function to the row of >> searchResults then link the id to a button(s) to either save, delete the >> associated row. >> But that''s my next step. You''ve helped enough. >> If I get stuck again, which I bet I will, I''ll post again. >> >> Thank you again. >> >> John > > Hmm.. Why not just do it the way the scaffolds set it up? :)-- Posted via http://www.ruby-forum.com/.