Forum: Ruby on Rails Problem with my show page

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Bef7651e6b8a2a4b5c37ece5783e0d34?d=identicon&s=25 Rik Stott (rikki123s)
on 2009-04-03 03:32
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:


<div align="center">Papers owned by the Author</div>

<table border="1" align="center" cellpadding="1" cellspacing="1"
bordercolor="#000033" bgcolor="#8A75FF">
  <tr>
    <td bgcolor="#CAC4FF">Paper</td>
  </tr>

  <% for paper in @papers %>
    <tr>
      <td><%=h paper.title %></td>
    </tr>
  <% end %>
</table>


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.
91a72b0ad3e43e3ef3a31816667ec694?d=identicon&s=25 bill walton (Guest)
on 2009-04-03 03:46
(Received via mailing list)
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
Bef7651e6b8a2a4b5c37ece5783e0d34?d=identicon&s=25 Rik Stott (rikki123s)
on 2009-04-03 03:59
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
Bef7651e6b8a2a4b5c37ece5783e0d34?d=identicon&s=25 Rik Stott (rikki123s)
on 2009-04-03 04:42
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 :) thanks for your aid Bill
91a72b0ad3e43e3ef3a31816667ec694?d=identicon&s=25 bill walton (Guest)
on 2009-04-03 05:41
(Received via mailing list)
Hi Rik,

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

> this works :) thanks for your aid Bill

You're welcome.  Glad you got it working.

Best regards,
Bill
This topic is locked and can not be replied to.