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 `photos' for #<User:0x007f88b07ccbe0>

Hello, I installed the gem devise and made the basic operations working and now I wanted to try to add new Photo model. One user can have only one photo.

When I open the URL: http://localhost:3000/users/1/photos/new

I get the undefined method 'photos' called on user object. I have tried many things but nothing helped. As I am beginner I am probably missing something.

Can anyone help please?

Thanks a lot, Miro

Photos Controller: <code> class PhotosController < ApplicationController before_action :find_user

def new
    @photo = @user.photos.new
end

def create
    @photo = @user.photos.new( photo_params )

    if @photo.save
        flash[:success] = "Your profile image has been uploaded."   
        redirect_to user_path(@user)
    else
        flash[:error] = "There was a problem uploading your profile photo."
        render action: :new
end
end

def edit
end

def update
end

private
def find_user
    @user = User.find( params[:user_id] )
end

def photos_params
params[:photo].permit(:image_file)

end

end

</code>

new.html.erb <code> <% form_for(:photo, @photo,:url=>{:action=>'create'}, :html=>{:multipart=>true}) do |f| %>

<%= f.file_field :image_file %>

<% end %> </code>

Database Scheme: <code> ActiveRecord::Schema.define(version: 20150716131518) do

create_table "photos", force: :cascade do |t| t.integer "user_id" t.string "description" t.string "content_type" t.string "filename" t.binary "binary_data" t.datetime "created_at", null: false t.datetime "updated_at", null: false end

add_index "photos", ["user_id"], name: "index_photos_on_user_id"

create_table "users", force: :cascade do |t| t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: false t.string "first_name" t.string "last_name" t.string "profile_name", default: "", null: false t.string "mobile_number" t.string "telephone_number" t.string "reset_password_token" t.datetime "reset_password_sent_at" t.datetime "remember_created_at" t.integer "sign_in_count", default: 0, null: false t.datetime "current_sign_in_at" t.datetime "last_sign_in_at" t.string "current_sign_in_ip" t.string "last_sign_in_ip" t.string "confirmation_token" t.datetime "confirmed_at" t.datetime "confirmation_sent_at" t.string "unconfirmed_email" t.datetime "created_at", null: false t.datetime "updated_at", null: false end

add_index "users", ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true add_index "users", ["email"], name: "index_users_on_email", unique: true add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true

end </code>