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

User_Id not saving in Pictures table

So I was having trouble getting the edit/delete to show up under my pictures and checked the database and noticed that the user_id is not being saved (it's nil)

I notice in find_user we call the profile_name, did I miss a step to get the user_id entered? I've checked my code with the source code provided.

Can anyone shed some light?

Here's my create method in pictures_controller.rb

def create
@picture = @album.pictures.new(params[:picture])

respond_to do |format|
  if @picture.save
    current_user.create_activity(@picture, 'created')
    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

And here's the find_user private method:

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

end

2 Answers

Lol it did solve the problem

You need to add:

@picture.user = current_user 

It will only work for newly created pictures. The older pictures will not be fixed. They have to be deleted manually.

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

Yeah, you have to add the @picture.user = current_user. That was my fix, but don't think I put it on this forum.

This worked for me too - thank you. I was using it to work through the beginning Rails course "Build a Simple Ruby on Rails App" (User and Status tables), but using rails 4 - it has taken some time (because of the version difference between Rails 4 and the course) but I am almost there. Thanks so much for this post - it was the last piece of the puzzle.

I do have one question though - does using this fix break the "Convention over configuration" approach that we should try to adhere to?

Jason Seifer
STAFF
Jason Seifer
Treehouse Guest Teacher

Do you have more in the pictures_controller? I don't think we have enough information to troubleshoot this one.

I have the same problem??? One thing that I did notice but doesn't seem to relate to my problem, in your videos you added one like of code after @picture = @album.pictures.new(params[:picture]) you inserted @pictures.user = current_user

making the overall controller look like this

pictures_controller.rb

def create
  @picture = @album.pictures.new(params[:picture])
  @pictures.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

Somebody please help me with this same problem :(