Rolify's role, I can only add a role in the terminal, but I can't add it from the view side

64 Views Asked by At

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 %>
0

There are 0 best solutions below