SageBeard / blockipedia

0 stars 1 forks source link

Collaborator #12

Closed SageBeard closed 5 years ago

SageBeard commented 7 years ago

I'm working on checkpoint 10 of Blockipedia. I'm trying to create a collaborator model where a premium user can select from a drop down menu of all users as their collaborator. I've set up my three models - user, wiki & collaborator - to have a HSMT relationship. My next step is to create a drop down 'collaborator' menu on the edit wiki form. I've set up a collaborator _form below, but am getting an error. I can't figure what I'm setting up wrong on my form.

screen shot 2017-08-05 at 7 07 17 pm

_form.html.erb

  <div class="col-md-8">
      <% User.all.each do |user| %>
      <div class="form-group">
            <%= f.label :collaborators, class: 'checkbox' do %>
            <%= f.select (:user, :user_id, User.all, :id) %>
      </div>
      <% end %>
    </div>
  <% end %>

Wiki Model

class Wiki < ActiveRecord::Base
  belongs_to :user
  has_many :collaborators
  has_many :collaborator_users, through: :collaborators

  validates :user, presence: true

  scope :visible_to, -> (user) do
     return all if user && (user.premium? || user.admin?)
     where(private: [false, nil])
  end

end

User Model

class User < ActiveRecord::Base

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  has_many :wikis
  has_many :collaborators
  has_many :collaborator_wikis, through: :collaborators

  enum role: [:standard, :premium, :admin]

  after_initialize :init, :if => :new_record?

  def init
    self.role  ||= :standard          #will set the default value only if it's nil
  end

end

Collaborator Model

class Collaborator < ActiveRecord::Base
  belongs_to :user
  belongs_to :wiki
end