Ruby 当资源不可用时,如何允许用户编辑他们创建的资源而不编辑其他资源';不属于用户?
在我使用的应用程序中,我拥有用户可以查看和创建商店的权限,但我也希望他们只能编辑他们创建的商店。用户可以创建任意数量的商店,所有商店都应该由他们编辑。商店没有用户,所以当Ruby 当资源不可用时,如何允许用户编辑他们创建的资源而不编辑其他资源';不属于用户?,ruby,ruby-on-rails-3,ruby-on-rails-3.1,cancan,Ruby,Ruby On Rails 3,Ruby On Rails 3.1,Cancan,在我使用的应用程序中,我拥有用户可以查看和创建商店的权限,但我也希望他们只能编辑他们创建的商店。用户可以创建任意数量的商店,所有商店都应该由他们编辑。商店没有用户,所以当store表中没有user\u id时,我怎么做 坎坎: class Ability include CanCan::Ability def initialize(user) user ||= User.new if user.role == "default" can :read, St
store
表中没有user\u id
时,我怎么做
坎坎:
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new
if user.role == "default"
can :read, Store
can :create, Store
end
end
end
我将在商店模型中添加以下内容:
has_one :created_by, :class => User
然后添加一个迁移,将创建的\u by \u id添加到存储类中
然后,您应该能够添加CanCan::功能:
can :edit, Store, :created_by => user
我将在商店模型中添加以下内容:
has_one :created_by, :class => User
然后添加一个迁移,将创建的\u by \u id添加到存储类中
然后,您应该能够添加CanCan::功能:
can :edit, Store, :created_by => user
由于用户可以创建任意数量的存储,因此存储将属于用户 您必须创建此关系 因此,在
用户
模型中
class User < ActiveRecord::Base
has_many :stores
end
class Store < ActiveRecord::Base
belongs_to :user
end
在ability.rb
文件中,只需输入如下内容:
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new # guest user (not logged in)
if user.role == 'default'
can :manage, Store , :user_id => user.id
end
end
end
由于用户可以创建任意数量的存储,因此存储将属于用户 您必须创建此关系 因此,在
用户
模型中
class User < ActiveRecord::Base
has_many :stores
end
class Store < ActiveRecord::Base
belongs_to :user
end
在ability.rb
文件中,只需输入如下内容:
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new # guest user (not logged in)
if user.role == 'default'
can :manage, Store , :user_id => user.id
end
end
end
我同意前面的一张海报,即您必须在
用户
和商店
之间建立关系。关系可以是一对多(如Kleber S.所示),也可以是多对多,如果一个商店可以有多个用户
然后,处理访问控制的最佳方法是在控制器中使用关联。对于show
、edit
、update
、destroy
方法,您需要在登录用户的情况下查找存储,因此请执行以下操作:
class StoresController < ApplicationController
before_filter :find_store, only: [:show, :edit, :update, :destroy]
def show
end
def edit
end
def update
if @store.update_attributes(params[:store])
# redirect to show
else
# re-render edit, now with errors
end
end
# ...
private
def find_store
@store = current_user.stores.find(params[:id])
end
end
class StoresController
通过这种方式,关联负责将查找限制为仅通过外键连接到当前用户的那些存储。这是RESTful Rails资源执行相关资源访问控制的标准方式。我同意前面的海报,即必须在
用户
和存储
之间建立关系。关系可以是一对多(如Kleber S.所示),也可以是多对多,如果一个商店可以有多个用户
然后,处理访问控制的最佳方法是在控制器中使用关联。对于show
、edit
、update
、destroy
方法,您需要在登录用户的情况下查找存储,因此请执行以下操作:
class StoresController < ApplicationController
before_filter :find_store, only: [:show, :edit, :update, :destroy]
def show
end
def edit
end
def update
if @store.update_attributes(params[:store])
# redirect to show
else
# re-render edit, now with errors
end
end
# ...
private
def find_store
@store = current_user.stores.find(params[:id])
end
end
class StoresController
通过这种方式,关联负责将查找限制为仅通过外键连接到当前用户的那些存储。这是RESTful Rails资源对相关资源执行访问控制的标准方法。如果一个
用户可以创建多个存储,我是否应该使用是否有一个?如果您希望能够抓取用户的存储,那么您应该放上“属于:创建人,:class=>user”。然后您可以执行User.find(…).stores或Store.find(…).created_by.我是否应该使用是否有一个如果一个用户可以创建多个门店
?是的,每个门店只有一个由用户创建的门店,对吗?如果您希望能够抓取用户的存储,那么您应该放上“属于:创建人,:class=>user”。然后,您可以执行User.find(…).stores或Store.find(…)。通过在模型中重写initialize
方法创建的方法是不可取的。正确的方法是在初始化后使用回调。这是因为ActiveRecord并不总是调用initialize
。不赞成在模型中重写initialize
方法。正确的方法是在初始化后使用回调。这是因为ActiveRecord并不总是调用initialize
。