Two tables, clickable row one table to fill other table

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

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.

Hrm. At the end, where I have :update => "matches" , for consistency's sake, please read :update => "searchresults".

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 :slight_smile:

-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.

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:

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.

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:

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.

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? :slight_smile:

Not a bad idea. I'll head in that direction and see what transpires.

Aldric Giacomoni wrote: