views:

36

answers:

1

class ProfileController < ApplicationController

def show

  username = params[:username]

  @user = User.find_by_username(username)
  if @user
    @title = "Profile for #{username}"
      user_id = @user.id

    @albums = Album.find(:all, :conditions => ["user_id = ?", user_id])

I have managed to display the index of albums for a given user on their profile page. Moving to the show action for an album displays the pictures in the given album. How can I put the show action of the album on the user's profile page?

A: 

Assume that you set up a partial for showing a single album.

app/views/albums/_display.html.erb

<h2><%= h album.name %></h2>
<p><%= h album.description %></p>

On the user page, you could display the albums by rendering that partial.

<% @albums.each do |album| -%>
  <%= render :partial => "/albums/display", :locals => {:album => album} %>
<% end -%>
jdl
That does not work. The show action should show the album's name among other things. Here is the error:undefined method `name' for nil:NilClass
chief
It was an example. I have no idea what your Albums show action or views look like, because you didn't post them. Show us what you're trying to do, and we can help you better.The error you posted indicates that you are not populating the @albums variable, despite what your code above shows.
jdl
@albums = Album.find(:all, :conditions => ["user_id = ?", user_id])This line in the controller is used to display the index of albums for a given user on their profile page. So I imagine I need something new in the controller to actually show the album on the profile, like @combined = @user.albums or whatever.
chief
You are confusing the concept of an action and a view. I would suggest a basic Rails tutorial first, then come back to this. You already have the @albums variable populated, so if you want these on the page, put them there.
jdl
Alright you were right. I was attempting to call the album name with an instance variable in the partial, thus giving me the undefined method. Thanks
chief