SageBeard / blockipedia

0 stars 1 forks source link

Collaborator #11

Closed SageBeard closed 5 years ago

SageBeard commented 7 years ago

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 wiki form. The menu would list out all users that the premium user can choose from to select or edit their collaborator for that wiki. I've set up my form below, but am having trouble figuring out how to properly set-up the collection_select method: '(object, method, collection, value_method, text_method, options = {}, html_options = {})'

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

_form.html.erb

<% if current_user.admin? || current_user.premium? %>
  <div class="form-group">
    <%= f.label :private, class: 'checkbox' do %>
      <%= f.check_box :private %> Private Wiki
    <% end %>
  </div>
  <% end %>

  <div class="col-md-8">
      <% User.all.each do |user| %>
      <div class="form-group">
      <%= f.label :collaborator, class: 'checkbox' do %>
      <%= f.collection_select(:user, :user_id, User.all, :id) %>
      <% end %>
    </div>
  <% end %>
bdougie commented 7 years ago

What is the trouble you are are having, are you getting an error, or is it not returning correct data?

Your question is set up nice, but it is very open ended and doesn't provide guidance on what assistance I can provide.

SageBeard commented 7 years ago

I'm getting an error with how I'm setting up my form below. My question is how to correctly set-up the collection_select method. I'm confused by the examples I've seen .

<%= f.label :collaborator, class: 'checkbox' do %>
      <%= f.collection_select(:user, :user_id, User.all, :id) %>
bdougie commented 7 years ago

Sorry this is still a bit open ended. Steps to reproduce the error is always needed as well as the actual error message.

I did the following to get this error but not 100% sure this is what you are looking at.

screenshot 2017-07-26 17 33 52

Reproduction Steps

  1. Login
  2. Visit /wikis/1/edit
  3. See the error

I googled it and found this and realized this is the wrong approach. https://stackoverflow.com/questions/10741810/collection-select-undefined-method-map-for-nilnilclass

Problem/Solution

  1. You need to use select not collection_select
  2. You need a collaborations view/partial, controller, and route.

app/views/collaborators/_collaborator_form.html.erb should host the form app/controllers/collaborators_controller.rb should have a create, destroy, and new method. Very similar to wikis controller.

Making these changes will put in the right direction.