Web开发新手,现在已经尝试解决了4天 - 已经通过StackOverflow上的每个解决方案,但没有一个答案对我有用。
我正在创建一个允许用户通过Paperclip上传附件和照片的应用程序。通过Devise和CanCan进行身份验证和授权
在编辑页面中,用户可以点击链接打开“编辑”按钮。页。从那里他可以看到一个表单来浏览并选择要上传的图像。点击按钮后,它会调用“更新”按钮。操作,然后呈现用户显示页面,图像保存到tmp文件夹,但不插入数据库。
这是我的用户模型
class User < ActiveRecord::Base
has_and_belongs_to_many :roles
has_attached_file :avatar, styles: {
thumb: '50x50>',
square: '200x200#',
medium: '300x300>'
}
def role?(role)
return !!self.roles.find_by_name(role.to_s.camelize)
end
# Validate the attached image is image/jpg, image/png, etc
validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, :confirmable,
:recoverable, :rememberable, :trackable, :validatable
end
用户控制器
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 do |format|
format.json { render :json => @users }
format.xml { render :xml => @users }
format.html
end
end
def new
respond_to do |format|
format.json { render :json => @users }
format.xml { render :xml => @users }
format.html
end
end
def show
@user = User.find(params[:id])
respond_to do |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 do |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 do |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 do |format|
format.json { render :json => @users.to_json, :status => 200 }
format.xml { head :ok }
format.html { redirect_to :action => :index }
end
else
respond_to do |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 you entered is incorrect" unless @user.valid_password?(params[:user][:current_password])
end
respond_to do |format|
if @user.errors[:base].empty? and @user.update_attributes(user_params[:user])
flash[:notice] = "Your account 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
以下是表单代码:
<%= form_for @user, :html => { :multipart => true } do |f| %>
<div class="field">
<%= f.label :avatar, "Photo" %>
<%= f.file_field :avatar %>
<%= f.submit "Submit" %>
</div>
<% end %>
我甚至无法显示临时图像。
非常感谢任何帮助!
Show.html.erb
<%= image_tag @user.avatar.url(:medium) %>
<%= @user.email %>