Ruby on rails 3 用户仅将自己的图像上载到配置文件
我在执行gems cancan时遇到问题,Desive和carrierwave在rails 3.2上 目前,我希望用户只能将图像上传到自己的用户页面(show)。目前,我有一个包含所有注册用户的用户索引页面。我可以进入其中的每一个页面,只需一个用户角色(不是管理员)就可以为每个配置文件添加一个图像。我只想能够为我自己的个人资料,并有效地只能阅读其他个人资料页(减去添加照片链接)。我的代码如下:Ruby on rails 3 用户仅将自己的图像上载到配置文件,ruby-on-rails-3,carrierwave,cancan,Ruby On Rails 3,Carrierwave,Cancan,我在执行gems cancan时遇到问题,Desive和carrierwave在rails 3.2上 目前,我希望用户只能将图像上传到自己的用户页面(show)。目前,我有一个包含所有注册用户的用户索引页面。我可以进入其中的每一个页面,只需一个用户角色(不是管理员)就可以为每个配置文件添加一个图像。我只想能够为我自己的个人资料,并有效地只能阅读其他个人资料页(减去添加照片链接)。我的代码如下: class Ability include CanCan::Ability def i
class Ability
include CanCan::Ability
def initialize(user)
# Define abilities for the passed in user here. For example:
#
user ||= User.new # guest user (not logged in)
if user.has_role? :user
can :read, User
can [:new, :create] , Photo, user_id: user.id
end
end
这就是CanCan的目的。您已经安装了它,但尚未对其进行配置
can:manage,Photo,user\u-id:user.id
另见:
此外,您需要确保您正在呼叫authorize代码>在您希望访问受限的每个控制器操作中。例如:
授权!:创建@photo
我更喜欢使用,以确保我没有错过任何行动,但只是使用authorize代码>将要求对代码进行最少的更改。我已经反复尝试了。。。使用不同的配置,似乎无法产生结果。我尝试了上面的方法…另一件事,我尝试了使用控制台查找所有具有用户角色的用户,但只找到了管理员。我的角色注册可能有问题吗?我已经反复阅读了文档。。。照片和用户之间的关联是否有问题。除了自己的用户档案,我看不到每个用户都禁用创建/新建照片。我已经编辑了上面我在我的能力中使用的内容。rb现在你授权的是更新或删除用户的能力,而不是更新照片的能力。另外,你没有授权创建操作。我根据你的建议对我目前的内容进行了编辑。它仍然不工作,但现在我不能添加照片到任何个人资料,这是进步,我想。。。我有什么遗漏吗?
class UsersController < ApplicationController
before_filter :authenticate_user!
def index
@users = User.all
end
def show
@user = User.find(params[:id])
@photo = Photo.new(params[:user_id])
end
end
class PhotosController < ApplicationController
before_filter :authenticate_user!
def new
@photo = Photo.new(:user_id => params[:user_id])
authorize! :new, @photo
end
def create
@photo = Photo.new(params[:photo])
if @photo.save
flash[:notice] = "Successfully created photo."
redirect_to @photo.user
else
render :action => 'new'
end
authorize! :create, @photo
end
def edit
@photo = Photo.find(params[:id])
end
def update
@photo = Photo.find(params[:id])
authorize! :update, @user
if @photo.update_attributes(params[:photo])
flash[:notice] = "Successfully updated photo."
redirect_to @photo.user
else
render :action => 'edit'
end
end
def destroy
authorize! :destroy, @user
@photo = Photo.find(params[:id])
@photo.destroy
flash[:notice] = "Successfully destroyed photo."
redirect_to @photo.user
end
end
ActiveRecord::Schema.define(:version => 20130512100231) do
create_table "photos", :force => true do |t|
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "image"
t.integer "user_id"
end
create_table "posts", :force => true do |t|
t.string "title"
t.text "body"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "roles", :force => true do |t|
t.string "name"
t.integer "resource_id"
t.string "resource_type"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
add_index "roles", ["name", "resource_type", "resource_id"], :name => "index_roles_on_name_and_resource_type_and_resource_id"
add_index "roles", ["name"], :name => "index_roles_on_name"
create_table "users", :force => true do |t|
t.string "email", :default => "", :null => false
t.string "encrypted_password", :default => "", :null => false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", :default => 0
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "name"
t.string "avatar"
t.string "image"
end
add_index "users", ["email"], :name => "index_users_on_email", :unique => true
add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true
create_table "users_roles", :id => false, :force => true do |t|
t.integer "user_id"
t.integer "role_id"
end
add_index "users_roles", ["user_id", "role_id"], :name => "index_users_roles_on_user_id_and_role_id"
end