0
votes

I let users to upload photos using paperclip, but there is no ownership in the photo. Which means, I don't know who uploaded the photos at all.

Here, I would like when someone uploads a picture you know which user uploaded it. i have a user_id column. but i dont know how to implement the code in the pic controller

How do I do that? Thanks!

class PicsController < ApplicationController
before_action :find_pic, only: [:show, :edit, :update, :destroy]

def index
    @user = User.find( params[:user_id])
    @pics = Pic.all.order("created_at DESC")

end

def show

end

def new
    @pic = Pic.new
end

def create
    @pic.user = current_user
    @pic = Pic.new(pic_params)
    if @pic.save
        redirect_to @pic, notice: "Yes it was posted"
    else 
        render 'new'
    end
end

def edit
    @user = User.find(params[:user_id])
    @profile = @user.profile
end

def update
    if @pic.update(pic_params)
        redirect_to @pic, notice: "Congrates Pic was upaded"
    else
        render 'new'
    end
end

def destroy
    @pic.destroy
    redirect_to users_path
end


private

def pic_params
    params.require(:pic).permit(:title, :description, :profile_id)
end

def find_pic
    @pic = Pic.find(params[:id])
  end

end

class UsersController < ApplicationController
before_action :authenticate_user!

def index
    @pics = Pic.all.order("created_at DESC")
end

def show @user = User.find(params[:id]) @pics = User.find_by(user_name: params[:user_name]) end

end

class ProfilesController < ApplicationController
before_action :authenticate_user!
before_action :only_current_user


def new
    @user = User.find( params[:user_id])
    @profile = Profile.new
end

def create
    @user = User.find(params[:user_id])
    @profile = @user.build_profile(profile_params)
    if @profile.save
        redirect_to user_path( params[:user_id])
    else
        render action: :new

    end
end

def edit
    @user = User.find(params[:user_id])
    @profile = @user.profile

end

def update
    @user = User.find(params[:user_id])
    @profile = @user.profile
    if @profile.update_attributes(profile_params)
        flash[:success] = "Profile Updated"
        redirect_to user_path(params[:user_id])
    else
       render action: :edit 
    end
end


private

def profile_params
    params.require(:profile).permit(:avatar, :user_name, :contact_email, :description)
end

def only_current_user
    @user = User.find(params[:user_id])
    redirect_to(root_url) unless @user == current_user
end

end

1
Can you please include your User and Pic models- so I can see how you're creating associations between the two entities in the database.JayJay

1 Answers

0
votes

If the user can be identified during the upload process, you can try to pass the user_id in a hidden_field during upload. You said you already created the user_id column.

<%= f.hidden_field :user_id , value: @user.id %>

for this code to work you need to find the @user in your controller action. Similar to what you are doing already in your 'index' action: find user by the :user_id If you are using devise for User you can use current_user.id instead.