[英]How do i only allow users that created a category to edit it in rails
I have two tables bookmarks
and categories
. 我有两个表
bookmarks
和categories
。 These models have a HABTM relationship. 这些模型具有HABTM关系。
Everything works as it should but I only want to allow users
who created a category
to be able to edit them. 一切都按预期工作,但我只想让创建
category
users
能够编辑它们。 I have this set up on the bookmarks
okay but is there a way to do it using the relationship between bookmarks
and categories
or do I just set it up the same way as bookmarks
? 我在
bookmarks
上设置好了,但有没有办法使用bookmarks
和categories
之间的关系来做到这一点,或者我只是像bookmarks
一样设置它? which is having a user_id
in the categories
table. 在
categories
表中有一个user_id
。
Assuming you have 2 join tables: 假设您有2个连接表:
user.rb user.rb
has_many :categories, :through => :user_categories
category.rb category.rb
has_many :users, :through => :user_categories
has_many :bookmarks, :through => :bookmark_categories
def is_editable_by?(user)
user.category.include? self
end
bookmark.rb bookmark.rb
has_many :categories, :through => :bookmark_categories
has_many :users, :through => :categories
your_view.html.erb your_view.html.erb
if category.is_editable_by? current_user
<%= link_to "edit", edit_category_path, category %>
end
If the intention is 如果是的话
a) only the user who created the bookmark can edit them a)只有创建书签的用户才能编辑它们
Then we need to have a separate column user_id
in bookmark
model to store the creator of the bookmark and allow access similar to category
. 然后我们需要在
bookmark
模型中有一个单独的列user_id
来存储bookmark
的创建者并允许类似于category
访问。
b) the user who created the category can edit any bookmarks under that category, then we can just use the associated to get the users with permissions. b)创建该类别的用户可以编辑该类别下的任何书签,然后我们可以使用关联来获得具有权限的用户。
bookmark.rb bookmark.rb
Class BookMark < ActiveRecord::Base
has_many :categories, :through => :bookmark_categories
has_many :users, :through => :categories
end
and we can just say bookmark.users.include?(current_user)
then allow edit only if this condition satisfies. 我们可以说
bookmark.users.include?(current_user)
然后只有在满足条件时才允许编辑。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.