Peter Kenvin
Peter Kenvin

Reputation: 11

Filtering Rails database

I have created a database of researchers and the papers they have written in Rails. I need to be able to filter it by author in such a way that information is drawn from the database onto a page in the persons name (for example if there is a researcher called Dr. A. Researcher, I need to be able to go to his page, and that page will be populated with papers that he/she has written automatically). I have been working on this for a while now, and have gone round in circles so many times, Im not exactly sure what I am looking for (though I think its probably going to be AJAX based). Filtering the database seems quite straightforward, but I cant seem to find any information about sending the results of that filter to a specific page.

As I say, Im not totally sure what Im looking for here, so someone may have had a similar issue that has already been answered. If that is the case, I apologise. Any help anyone has about this would be very gratefully received.

Cheers

Upvotes: 0

Views: 177

Answers (1)

Purple Hexagon
Purple Hexagon

Reputation: 3568

Seems like you would just want to use the show action.

In your controllers/researchers.rb controller add

def show
  @reseacher = Reseacher.find(params[:id])
end

or possibly

def show
    @reseacher = Reseacher.find(params[:id], include: [:papers])
end

then in your views/show.html.erb view you can just add

<% @reseacher.papers.each do |p| %>
    <%= p.title %>
    <%= p.otherattribute %>
<% end %>

Also you will need to ensure your relationships are setup correctly in your model

in models/researcher.rb

add

has_many :papers

to the Researcher Class

and in models/papers.rb

add

belongs_to :researcher

to the Paper Class

localhost:3000/reseachers/1 where 1 is the id of the researcher should then return the papers for that researcher

Upvotes: 1

Related Questions