简体   繁体   English

我如何只允许创建类别的用户在rails中编辑它

[英]How do i only allow users that created a category to edit it in rails

I have two tables bookmarks and categories . 我有两个表bookmarkscategories 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上设置好了,但有没有办法使用bookmarkscategories之间的关系来做到这一点,或者我只是像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.

相关问题 如何编辑.htaccess以允许rails和wordpress请求? - How do I edit .htaccess to allow both rails and wordpress requests? Rails:允许用户仅查看或编辑自己的内容 - Rails: Allow users to only view or edit their own content rails可以允许用户“仅”编辑数据,但查看所有人 - rails cancan allow users to edit *only* their data, but view everyones 在Rails4中使用Devise,如何允许用户仅编辑自己的注释? - Using Devise in Rails4, how to allow users to only the edit their own comments? Ruby-Rails如何仅允许当前用户编辑和删除他们创建的帖子 - Ruby-On-Rails How to allow only current user to edit and delete posts they have created 如何只允许已登录的用户查看图像 - How do I allow only logged in users to view images 如何在Rails 4的生产环境中允许用户编辑动态苗条页面模板? - How to allow users to edit dynamic slim page templates in production for rails 4? 如果文章是自己的,如何让用户只能看到编辑和删除链接? - How to allow users to only see edit and delete link if the article is their own? 在Rails 4模型中,如何只允许使用范围来管理具有特定值的用户? - How do allow to use a scope only to admin users with a certain value in Rails 4 model? 设计:允许管理员编辑其他用户 - Rails - Devise: Allow admins to edit other users - Rails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM