Tuesday 15 March 2011

ruby on rails 4 - Paperclip not saving photo to Database -



ruby on rails 4 - Paperclip not saving photo to Database -

new web development , have been trying solve 4 days - been through every solution here on stackoverflow none of answers have worked me.

i creating application allows users upload attachment , photo through paperclip. authentication , authorization through devise , cancan

from edit page, user can click on link open 'edit' page. there can see form browse , take image upload. when clicking on button, calls 'update' action , renders users show page, image saved tmp folder not insert database.

here user model

class user < activerecord::base has_and_belongs_to_many :roles has_attached_file :avatar, styles: { thumb: '50x50>', square: '200x200#', medium: '300x300>' } def role?(role) homecoming !!self.roles.find_by_name(role.to_s.camelize) end # validate attached image image/jpg, image/png, etc validates_attachment_content_type :avatar, :content_type => /\aimage\/.*\z/ # include default devise modules. others available are: # :confirmable, :lockable, :timeoutable , :omniauthable devise :database_authenticatable, :registerable, :confirmable, :recoverable, :rememberable, :trackable, :validatable end

user controller

class userscontroller < applicationcontroller before_action :get_user, :only => [:index, :new, :edit] before_action :accessible_roles, :only => [:new, :edit, :show, :update, :create] load_and_authorize_resource :only => [:show, :new, :destroy, :edit, :update] def index @users = user.accessible_by(current_ability, :index).limit(20) respond_to |format| format.json { render :json => @users } format.xml { render :xml => @users } format.html end end def new respond_to |format| format.json { render :json => @users } format.xml { render :xml => @users } format.html end end def show @user = user.find(params[:id]) respond_to |format| format.json { render :json => @users } format.xml { render :xml => @users } format.html end rescue activerecord::recordnotfound respond_to_not_found(:json, :xml, :html) end def edit respond_to |format| format.json { render :json => @users } format.xml { render :xml => @users } format.html end rescue activerecord::recordnotfound respond_to_not_found(:json, :xml, :html) end def destroy @user.destroy! respond_to |format| format.json { respond_to_destroy(:ajax) } format.xml { head :ok } format.html { respond_to_destroy(:html) } end rescue activerecord::recordnotfound respond_to_not_found(:json, :xml, :html) end def create @user = user.new(user_params) if @user.save respond_to |format| format.json { render :json => @users.to_json, :status => 200 } format.xml { head :ok } format.html { redirect_to :action => :index } end else respond_to |format| format.json { render :text => "could not create user", :status => :unprocessable_entity } format.xml { head :ok } format.html { render :action => :new, :status => :unprocessable_entity } end end end def update if params[:user][:password].blank? [:password, :password_confirmation, :current_password].collect{|p| params[:user].delete(p) } else @user.errors[:base] << "the password entered incorrect" unless @user.valid_password?(params[:user][:current_password]) end respond_to |format| if @user.errors[:base].empty? , @user.update_attributes(user_params[:user]) flash[:notice] = "your business relationship has been updated" format.json { render :json => @user.to_json, :status => 200 } format.xml { head :ok } format.html { render :action => :show } else format.json { render :text => "could not update user", :status => :unprocessable_entity } format.xml { render :xml => @user.errors, :status => :unprocessable_entity } format.html { render :action => :edit, :status => :unprocessable_entity } end end rescue activerecord::recordnotfound respond_to_not_found(:json, :xml, :html) end private def accessible_roles @accessible_roles = role.accessible_by(current_ability, :read) end def get_user @user = current_user end def user_params params.require(:user).permit(:name, :email, :password, :password_confirmation, :avatar, :resume) end end

here form code:

<%= form_for @user, :html => { :multipart => true } |f| %> <div class="field"> <%= f.label :avatar, "photo" %> <%= f.file_field :avatar %> <%= f.submit "submit" %> </div> <% end %>

i can not seem display temp image.

any help much appreciated!

show.html.erb

<%= image_tag @user.avatar.url(:medium) %> <%= @user.email %>

ruby-on-rails-4 amazon-s3 paperclip

No comments:

Post a Comment