Rolify的角色,我只能在终端中添加一个角色,但是我可以从视图侧添加它

发布于 2025-02-13 02:20:28 字数 1511 浏览 2 评论 0原文

我可以在终端中添加角色,但是我无法从视图侧添加它。 我可以使用终端添加Postuser模型的角色:

PostUser.first.add_role :admin
PostUser.first.has_role? :admin
=> true;

我尝试从视图端添加它:

doutes.rb file:

resources :posts do
  resources :post_users
end

post_user.rb模型文件:

class PostUser < ApplicationRecord
  rolify
  belongs_to :post

  after_create :assign_default_role

   def assign_default_role
     self.add_role(:newuser) if self.roles.blank?
   end
end

add_role: post_user.rb的add_role模型文件不起作用!

post.rb模型文件:

class Post < ApplicationRecord
   resourcify

   belongs_to :user
   has_many :post_users, dependent: :destroy
end

user.rb模型文件:

class User < ApplicationRecord
   # Include default devise modules. Others available are:
   # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
   resourcify

   devise :database_authenticatable, :registerable,
          :recoverable, :rememberable, :validatable
   has_many :posts, dependent: :destroy
end

我还尝试了resourcify on post.rb和<代码> user.rb ,但它不起作用。

我从表格中添加的内容也无法正常工作。 编辑文件:

<%= form_with(model: [post, post_user]) do |form|%>
   <%= form.collection_check_boxes :role_ids, Role.all, :id, :name %>
   <%= form.button :submit %>
<% end %>

I can added role with terminal, but I can't add it from the view side.
I can added role for PostUser model with terminal:

PostUser.first.add_role :admin
PostUser.first.has_role? :admin
=> true;

I tried to add this from the view side:

routes.rb file:

resources :posts do
  resources :post_users
end

post_user.rb model file:

class PostUser < ApplicationRecord
  rolify
  belongs_to :post

  after_create :assign_default_role

   def assign_default_role
     self.add_role(:newuser) if self.roles.blank?
   end
end

add_role of post_user.rb model file not working!

post.rb model file:

class Post < ApplicationRecord
   resourcify

   belongs_to :user
   has_many :post_users, dependent: :destroy
end

user.rb model file:

class User < ApplicationRecord
   # Include default devise modules. Others available are:
   # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
   resourcify

   devise :database_authenticatable, :registerable,
          :recoverable, :rememberable, :validatable
   has_many :posts, dependent: :destroy
end

I also tried resourcify on post.rb and user.rb but it doesn't work.

What I added from the form didn't work either. edit file:

<%= form_with(model: [post, post_user]) do |form|%>
   <%= form.collection_check_boxes :role_ids, Role.all, :id, :name %>
   <%= form.button :submit %>
<% end %>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文