Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

SOLVED - Unable to view albums

After creating the Albums and Pictures and following the steps in the Creating Albums and Pictures and Listing Pictures videos from Advanced Social Features, I keep running into the same error when creating a new album and then trying to view the album:

NoMethodError in Pictures#index

Showing     /Users/[removed]/Documents/Programming/Projects/treebook/app/views/pictures/index.html.erb where line #3 raised:

undefined method `merger' for {:profile_name=>"[omitted]"}:Hash
Extracted source (around line #3):

1: <%= page_header do %>
2:   <% if signed_in? && current_user == @user %>
3:     <%= link_to 'Add Picture', new_album_picture_path(current_user, @album), class: 'btn pull-right' %>
4:   <% end %>
5:   <h1><%= @album.title %></h1>
6: <% end %>

I've deleted all albums in the Rails console, reset/migrated the database with no change.

I don't see anything in any of the videos mentioning a "merger" method.

Any ideas?

7 Answers

I figured it out. Stupid spelling/syntax error (as usual)

def url_options
    { profile_name: params[:profile_name] }.merger(super)
end

Should be:

def url_options
    { profile_name: params[:profile_name] }.merge(super)
end

Thank you for your help!

Is your project on github? It's hard to answer this question without more code.

Emilia, here's the index page:

<%= page_header do %>
  <% if signed_in? && current_user == @user %>
    <%= link_to 'Add Picture', new_album_picture_path(current_user, @album), class: 'btn pull-right' %>
  <% end %>
  <h1><%= @album.title %></h1>
<% end %>

<ul class="thumbnails">
<% @pictures.each do |picture| %>
  <li>
    <div class="thumbnail">
    <%= link_to image_tag(picture.asset.url(:small)), album_picture_path(@user, @album, picture) %><br />
      <div class="caption">
        <% if picture.caption? %><%= picture.caption %><br /><% end %>

        <%= link_to 'View full size', album_picture_path(@user, @album, picture) %>

        <% if can_edit_picture?(picture) %>
          | <%= link_to "Edit", edit_album_picture_path(@album, picture) %>
          | <%= link_to "Delete", album_picture_path(@album, picture), method: :delete, data: { confirm: "Are you sure?" } %>
        <% end %>

      </div>
    </div>
  </li>  
<% end %>
</ul>

Seems to be on line 3.

Here is the server log:

Started GET "/[omitted]/albums/4/pictures" for 127.0.0.1 at 2013-12-18 14:22:57 -0500
Processing by PicturesController#index as HTML
  Parameters: {"profile_name"=>"[omitted]", "album_id"=>"4"}
  User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."profile_name" = '[omitted]' LIMIT 1
  User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
  Album Load (0.1ms)  SELECT "albums".* FROM "albums" WHERE "albums"."user_id" = 1 AND "albums"."id" = ? LIMIT 1  [["id", "4"]]
  Picture Load (0.1ms)  SELECT "pictures".* FROM "pictures" 
  Rendered pictures/index.html.erb within layouts/application (1.8ms)
Completed 500 Internal Server Error in 8ms

What about your controller?

class PicturesController < ApplicationController
  # GET /pictures
  # GET /pictures.json
  before_filter :authenticate_user!, only: [:new, :create, :edit, :update, :destroy]
  before_filter :find_user
  before_filter :find_album

  def index
    @pictures = Picture.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @pictures }
    end
  end

  # GET /pictures/1
  # GET /pictures/1.json
  def show
    @picture = Picture.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @picture }
    end
  end

  # GET /pictures/new
  # GET /pictures/new.json
  def new
    @picture = Picture.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @picture }
    end
  end

  # GET /pictures/1/edit
  def edit
    @picture = Picture.find(params[:id])
  end

  # POST /pictures
  # POST /pictures.json
  def create
    @picture = @album.pictures.new(params[:picture])
    @picture.user = current_user

    respond_to do |format|
      if @picture.save
        format.html { redirect_to album_pictures_path(@album), notice: 'Picture was successfully created.' }
        format.json { render json: @picture, status: :created, location: @picture }
      else
        format.html { render action: "new" }
        format.json { render json: @picture.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /pictures/1
  # PUT /pictures/1.json
  def update
    @picture = Picture.find(params[:id])

   respond_to do |format|
      if @picture.update_attributes(params[:picture])
        format.html { redirect_to @picture, notice: 'Picture was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @picture.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /pictures/1
  # DELETE /pictures/1.json
  def destroy
    @picture = Picture.find(params[:id])
    @picture.destroy

    respond_to do |format|
      format.html { redirect_to pictures_url }
      format.json { head :no_content }
    end
  end

  def url_options
    { profile_name: params[:profile_name] }.merger(super)
  end

  private
  def find_user
    @user = User.find_by_profile_name(params[:profile_name])
  end

  def find_album
    if signed_in?
      @album = current_user.albums.find(params[:album_id])
    else
      @album = @user.albums.find(params[:album_id])
    end
  end

  def find_picture
    @picture = @album.pictures.find(params[:picture_id])
  end
end