Rolify的角色,我只能在终端中添加一个角色,但是我可以从视图侧添加它
我可以在终端中添加角色,但是我无法从视图侧添加它。 我可以使用终端添加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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论