Problem with my show page

Ok I have now got my papers to be able to have an author be selected for
them from a drop down box generated in the new paper page
‘papers/new.html.erb’, which was fixed in an earlier post.

Now I have a problem on the other side of the site, I now want to
display a table in the authors show page ‘authors/show.html.erb’, that
displays all the papers made by the specific author shown in the authors
show page.

How I plan to do this is by getting the authors controller to only pick
from the papers table where the papers.author field = the authors.name
field (both fields being the author name).

So far I have the table set out in the authors/show.html.erb page like
this:

Papers owned by the Author

<% for paper in @papers %>




<% end %>
Paper
<%=h paper.title %>

And so far the show method for my author controller is displayed as so:

def show
@author = Author.find(params[:id])
@papers = Paper.find(:all)

respond_to do |format|
  format.html # show.html.erb
  format.xml  { render :xml => @author }
  format.xml  { render :xml => @papers }
end

end

Obviously all this is going to do is show all the papers in the papers
table, rather than the ones whos author = the author name in the authors
table.

Could anyone help me with this? I beleive that the change needs to be
made in the ‘@papers = Paper.find(:all)’ line in the controller, i just
do not know what to replace this line with.

Thanks for any replies.

Hi Rik,

On Fri, 2009-04-03 at 03:32 +0200, Rik Stott wrote:

I beleive that the change needs to be made in the
@papers = Paper.find(:all)’ line in the controller,
i just do not know what to replace this line with.

I didn’t see your other posts so I’m not sure what your associations
look like but assuming it’s:

Paper -> belongs_to :author

@papers = Paper.find_all_by_author_id(params[:id]

and then you can get rid of the find on Author.

HTH,
Bill

Hey Bill,

I tried that, but it came out with an error saying:

“undefined method `find_all_by_author_id’ for #Class:0x7380e94

I cannot delete the find on the author because that is required for a
different table on the page.

I then realised it should have been ‘find_all_by_author’, because the
field in the papers page is called ‘author’, not author_id.

the problem I now have is that the page will display, with the table,
but there is no records being shown.

the author field in the authors table is named ‘name’, should i change
this to ‘author’ also?

thanks for your help

bill walton wrote:

Hi Rik,

On Fri, 2009-04-03 at 03:32 +0200, Rik Stott wrote:

I beleive that the change needs to be made in the
@papers = Paper.find(:all)’ line in the controller,
i just do not know what to replace this line with.

I didn’t see your other posts so I’m not sure what your associations
look like but assuming it’s:

Paper -> belongs_to :author

@papers = Paper.find_all_by_author_id(params[:id]

and then you can get rid of the find on Author.

HTH,
Bill

I’ve sussed it.

by now I have changed the field in paper from :author, to :name. I’ve
done a lot of work and can’t be bothered back tracking everything for
just the sake of changing it back. it works like this anyway so it
doesn’t matter.

once I had changed the name of the authors field on the papers table to
:name, the code I typed into the controller to replace the line was:

@papers = Paper.find_all_by_name(params = @author[:name])”

this works :slight_smile: thanks for your aid Bill

Hi Rik,

On Fri, 2009-04-03 at 04:42 +0200, Rik Stott wrote:

this works :slight_smile: thanks for your aid Bill

You’re welcome. Glad you got it working.

Best regards,
Bill

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