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:

<%= 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”.

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

<%= link_to_remote "Find matches", :update => "matches", :url => { :controller => "comparison", :action => "find_matches", :id =>

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…

<%= link_to_remote "Find Matches", :update => "projects", :url => {:controller => 'projects', :action => 'search', :id => import.import_id } %>

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 M. 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 G. 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:

<%= link_to_remote "Find matches", :update => "matches", :url => { :controller => "comparison", :action => "find_matches", :id =>

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

John M. 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 } %>

  <% @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’ %>

<% end %>

Search Results
Project ID IRB ID Title Pi full name Action

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

Listing imports

<% form_remote_tag :update => 'project', :url => { :action => 'search' } do %>
  <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’ %>

<%= link_to_remote “Find Matches”, :update =>
“search_results”, :url => {:controller => ‘projects’, :action =>
‘search’, :id => import.irb_id } %>
      </tr>
    <% end %>
  </table>

</div>

<% end %>

  <% @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’ %>

<% end %>

Search Results
Project ID IRB ID Title Pi full name Action

Thanks again.
John

Aldric G. wrote:

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

You probably want to see my partial

  • results.html.erb -
<% @projects.each do |project| %> <% end %>
Project
Project ID IRB ID Title Pi full name Action
<%=h project.irb_id %> <%=h project.project_id %> <%=h project.title %> <%=h project.pi_full_name %> <%= link_to 'view', project, :rel => 'facebox' %>

John

John M. 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 G. wrote:

John M. 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 } %>

  <% @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’ %>

<% end %>

Search Results
Project ID IRB ID Title Pi full name Action

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.

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 M. wrote:

  • results.html.erb -
<% @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.

Project
Project ID IRB ID Title Pi full name Action

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 G. wrote:

John M. 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 } %>

  <% @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’ %>

<% end %>

Search Results
Project ID IRB ID Title Pi full name Action

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,
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 G. 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 M. wrote:

  • results.html.erb -
<% @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.

Project
Project ID IRB ID Title Pi full name Action

John M. 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? :slight_smile:

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 G. wrote:

John M. 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? :slight_smile:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs