ruby on rails - When I update my Avatar picture, it changes all users picture -


i seem making error in code. i've been looking @ 2 of past projects , in 1 have pictures table associate picture_id user on different project wasn't required , forgot how did it. advice in code improvement , reason of error appreciated, thank in advance.

schema

create_table "users", force: true |t|     t.string   "first_name"     t.string   "last_name"     t.string   "email"     t.string   "password_digest"     t.string   "user_name"     t.date     "birthdate"     t.integer  "zip_code"     t.string   "gender"     t.datetime "created_at"     t.datetime "updated_at"     t.string   "avatar_file_name"     t.string   "avatar_content_type"     t.integer  "avatar_file_size"     t.datetime "avatar_updated_at"     t.integer  "user_id"     t.integer  "profile_id"     t.string   "background_file_name"     t.string   "background_content_type"     t.integer  "background_file_size"     t.datetime "background_updated_at"     t.string   "slug"   end 

user/show

<div id="profile_to">  <div class="profile_background_picture">   <%= image_tag current_user.background.url(:medium) %>  </div>  <div class="profile_picture">   <%= image_tag current_user.avatar.url(:medium) %>  </div> </div> 

user/index

<% if current_user %>  <div id="login_top">   <a href="/"><p class="logo">trend</p></a>   <nav>    <div class="loginout">     <%= link_to ("logout"), "/sessions/new",method: :delete %>    </div>    <div class="user-links">     <a href="/users/<%= current_user.id %>">      <% if current_user.user_name.present? %>       <%= link_to current_user.user_name, user_path(current_user) %>      <% else %>       <%= current_user.first_name %>      <% end %>     </a>     &nbsp;<b class="size">|</b>&nbsp;     <a href="">settings</a>     &nbsp;<b class="size">|</b>&nbsp;    </div>    <% if current_user.avatar.present? %>     <div class="circular">      <%= image_tag current_user.avatar.url(:medium) %>     </div>    <% else %>         <% end %>   </nav>  </div>  <% end %>  <%= form_for current_user, :html => { :multipart => true } |f| %>   <p class="editpage">profile picture: <%= f.file_field :avatar %></p>   <%= f.submit "upload" %> <% end %> 

user model

has_attached_file :avatar, :styles => {  :medium => "200x200>",  :small => "120x120#",  :thumb => "75x75#",  :default_url => "http://www.adtechnology.co.uk/images/ugm-default-user.png" }  validates_attachment_content_type :avatar, :content_type => /\aimage\/.*\z/  has_attached_file :background, :styles => {  :medium => "200x200>",  :small => "120x120#",  :thumb => "75x75#",  :default_url => "http://www.adtechnology.co.uk/images/ugm-default-user.png" }  validates_attachment_content_type :background, :content_type => /\aimage\/.*\z/ 

for further information, please feel free ask. again, thank , explanation error.

user controller

def index  @user = user.new  @users = user.all end  def create @user = user.new(user_params)  if @user.save   session[:user_id] = @user.id   cookies[:user_id] = @user.id   flash[:notice] = "successfully registerd"   redirect_to "/"  else   flash[:alert] = @user.errors.full_messages   redirect_to "/"  end end  def new  @user = user.new end  def edit  @user = user.friendly.find(params[:id])  current_user end  def show  @user = user.friendly.find(params[:id])  current_user end  def update  @user = user.friendly.find(params[:id])   if @user.update(user_params)    flash[:notice] = "you have update information"    redirect_to "/"  end end  def destroy  @user = user.friendly.find(params[:id])  @user.destroy end  private  def user_params  params.require(:user).permit(:background, :username_or_email, :first_name,  :last_name, :email, :password, :password_confirmation, :user_name, :female, :male, :avatar, :gender, :zip_code, :birthdate) end 

your approach has pungent code smell; not see reason user model have t.integer "user_id" column, when you've set asset directly on user model. poor, , brittle, approach you've lumped images user.

when building feature this, it's attempt normalising domain model, , 1 approach have 2 tables - users , user_avatars. way, user_avatars table can have user_id column used foreign key. user instance has_one :avatar_image, class_name: "::useravatar" , useravatar belong_to :user.

this allow user instance (i.e. user = user.new; i'm showing it's instance of user klass) able call user.avatar_image; , if had has_attached_file :avatar declaration on useravatar, you'll able fetch via user.avatar_image.avatar.url(:thumb)


Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

python - build a suggestions list using fuzzywuzzy -