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

Undefined Method Captain

Error

NoMethodError in Pictures#new

Showing C:/Sites/MSN/app/views/pictures/_form.html.erb where line #7 raised:

undefined method `caption' for #<Picture:0x44a6c48>
Extracted source (around line #7):

4:   <div class="form-inputs">
5:     <%= f.input :asset, as: :file %>
6:     <%= bootstrap_paperclip_picture f, :asset %>
7:     <%= f.input :caption, input_html: { class: 'input-xxlarge' } %>
8:     <%= f.input :description, input_html: { class: 'input-xxlarge', rows: 4 } %>
9:   </div>
10: 
Trace of template inclusion: app/views/pictures/new.html.erb

Rails.root: C:/Sites/MSN

Application Trace | Framework Trace | Full Trace
app/views/pictures/_form.html.erb:7:in `block in _app_views_pictures__form_html_erb__27104755_45926568'
app/views/pictures/_form.html.erb:1:in `_app_views_pictures__form_html_erb__27104755_45926568'
app/views/pictures/new.html.erb:3:in `_app_views_pictures_new_html_erb___1059767986_45833832'
app/controllers/pictures_controller.rb:36:in `new'
Request

Parameters:

{"profile_name"=>"srake",
 "album_id"=>"1"}
Show session dump

Show env dump

Response

Headers:

None

app/controllers/pictures_controller.rb

class PicturesController < ApplicationController
  before_filter :authenticate_user!, only: [:new, :create, :edit, :update, :destroy]
  before_filter :find_user
  before_filter :find_album
  before_filter :find_picture, only: [:edit, :update, :show, :destroy]
  before_filter :ensure_proper_user, only: [:edit, :new, :create, :update, :destroy]
  before_filter :add_breadcrumbs

  # GET /pictures
  # GET /pictures.json
  def index
    @pictures = @album.pictures.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
    add_breadcrumb @picture, album_picture_path(@album, @picture)

    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 = @album.pictures.new

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

  # GET /pictures/1/edit
  def edit

  end

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

    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
    respond_to do |format|
      if @picture.update_attributes(params[:picture])
        format.html { redirect_to album_pictures_path(@album), 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.destroy

    respond_to do |format|
      format.html { redirect_to album_pictures_url(@album) }
      format.json { head :no_content }
    end
  end

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

  private
  def ensure_proper_user
    if current_user != @user
      flash[:error] = "You don't have permission to do that."
      redirect_to album_pictures_path
    end
  end

  def add_breadcrumbs
    add_breadcrumb @user.first_name, profile_path(@user)
    add_breadcrumb "Albums", albums_path
    add_breadcrumb "Pictures", album_pictures_path(@album)
  end

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

  def find_album
    if signed_in? && current_user.profile_name == params[:profile_name]
      @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[:id])
  end
end

app/views/pictures/index.html.erb

<%= 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>

app/views/pictures/_form.html.erb

<%= simple_form_for([@album, @picture], html: { class: 'form-horizontal' }) do |f| %>
  <%= f.error_notification %>

  <div class="form-inputs">
    <%= f.input :asset, as: :file %>
    <%= bootstrap_paperclip_picture f, :asset %>
    <%= f.input :caption, input_html: { class: 'input-xxlarge' } %>
    <%= f.input :description, input_html: { class: 'input-xxlarge', rows: 4 } %>
  </div>

  <div class="form-actions">
    <%= f.button :submit %>
  </div>
<% end %>

app/helpers/pictures_helper.rb

module PicturesHelper
  def can_edit_picture?(picture)
    signed_in? && current_user == picture.user
  end
end

I am completely lost. Please help me. :o

I even tried downloading and running the project files and this error still occurred? I'm lost... I did rake db:reset and rake db:migrate. Lola I just don't understand

1 Answer

Lol I'm stupid, Solved it by replacing "Captain" with "Caption" ...