Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CollaboratorsController.rb
- ============================
- class CollaboratorsController < ApplicationController
- before_action :authenticate_user!
- def create
- wiki = Wiki.find(params[:wiki_id])
- collaborator = current_user.collaborators.build(wiki: wiki)
- if collaborator.save
- flash[:notice] = "Collaborator created"
- else
- flash[:alert] = "Collaborator creation failed."
- end
- redirect_to wikis_path
- end
- def destroy
- wiki = Wiki.find(params[:id])
- collaborator = current_user.collaborators.where(wiki: wiki).create
- if collaborator.destroy
- flash[:notice] = "Removed collaborator."
- else
- flash[:alert] = "Collaborator removal failed."
- end
- redirect_to wikis_path
- end
- end
- _collaborator.html.erb
- ==========================
- <div class="form-group">
- <%# this should more properly use a variable set in your controller rather than User.all - for example you might want to limit the list of possible collaborators according to some condition %>
- <% User.all.each do |user| %>
- <% #if collaborator = user.collaborator_for(wiki) %>
- <% if wiki.private? && user != wiki.user && !wiki.collaborators.include?(user) %>
- <div class="user">
- <%= user.name %>
- <%= link_to "make collaborator", collaborators_path(wiki_id: wiki.id), class: 'btn btn-primary', method: :post %>
- </div>
- <% end %>
- <% if wiki.private? && user != wiki.user && wiki.collaborators.include?(user) %>
- <%= user.name %>
- <%= link_to "Remove_collaborator", collaborator_path(wiki), class: 'btn btn-danger', method: :delete %>
- <%#= link_to "delete collaborator", [wiki, collaborator], method: :delete do %>
- <% end %>
- <% end %>
- </div>
- rails console output:
- =======================
- sharadalt:~/workspace (wiki_collaborators) $ rails c
- [Simple Form] Simple Form is not configured in the application and will use the default values. Use `rails generate simple_form:install` to generate the Simple Form configuration.
- Loading development environment (Rails 4.2.4)
- [1] pry(main)> wiki = Wiki.last
- Wiki Load (0.3ms) SELECT "wikis".* FROM "wikis" ORDER BY "wikis"."id" DESC LIMIT 1
- => #<Wiki:0x00000005dbb5f0
- id: 11,
- title: "wiki user2",
- body: "bbbbbbbb",
- private: true,
- user_id: 2,
- created_at: Wed, 30 Mar 2016 06:16:22 UTC +00:00,
- updated_at: Wed, 30 Mar 2016 06:16:22 UTC +00:00>
- [2] pry(main)> wiki.user
- User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
- => #<User:0x00000005d366c0
- id: 2,
- name: "user2",
- email: "user2@example.com",
- encrypted_password: "$2a$10$qREytQJfrjMp.O4dvuHaMuARBs4ZOdCyM/1yjxUonGneEz8NjobOW",
- reset_password_token: nil,
- reset_password_sent_at: nil,
- remember_created_at: nil,
- sign_in_count: 1,
- current_sign_in_at: Wed, 30 Mar 2016 06:15:05 UTC +00:00,
- last_sign_in_at: Wed, 30 Mar 2016 06:15:05 UTC +00:00,
- current_sign_in_ip: "69.181.65.0",
- last_sign_in_ip: "69.181.65.0",
- confirmation_token: "KogTgcBwNGGcDfhyDWuz",
- confirmed_at: Wed, 30 Mar 2016 05:54:19 UTC +00:00,
- confirmation_sent_at: Wed, 30 Mar 2016 05:54:18 UTC +00:00,
- unconfirmed_email: nil,
- failed_attempts: 0,
- [3] pry(main)>
- [4] pry(main)> wiki.collaborators
- Collaborator Load (0.3ms) SELECT "collaborators".* FROM "collaborators" WHERE "collaborators"."wiki_id" = ? [["wiki_id", 11]]
- => [#<Collaborator:0x00000005a842d8
- id: 1,
- user_id: 2,
- wiki_id: 11,
- created_at: Wed, 30 Mar 2016 06:16:31 UTC +00:00,
- updated_at: Wed, 30 Mar 2016 06:16:31 UTC +00:00>]
- [5] pry(main)> current_user
- NameError: undefined local variable or method `current_user' for main:Object
- from (pry):4:in `__pry__'
- [6] pry(main)> wiki.user
- => #<User:0x00000005d366c0
- id: 2,
- name: "user2",
- email: "user2@example.com",
- encrypted_password: "$2a$10$qREytQJfrjMp.O4dvuHaMuARBs4ZOdCyM/1yjxUonGneEz8NjobOW",
- reset_password_token: nil,
- reset_password_sent_at: nil,
- remember_created_at: nil,
- sign_in_count: 1,
- current_sign_in_at: Wed, 30 Mar 2016 06:15:05 UTC +00:00,
- last_sign_in_at: Wed, 30 Mar 2016 06:15:05 UTC +00:00,
- current_sign_in_ip: "69.181.65.0",
- last_sign_in_ip: "69.181.65.0",
- [7] pry(main)> wiki.user.collaborators
- Collaborator Load (0.3ms) SELECT "collaborators".* FROM "collaborators" WHERE "collaborators"."user_id" = ? [["user_id", 2]]
- => [#<Collaborator:0x00000005904fc0
- id: 1,
- user_id: 2,
- wiki_id: 11,
- created_at: Wed, 30 Mar 2016 06:16:31 UTC +00:00,
- updated_at: Wed, 30 Mar 2016 06:16:31 UTC +00:00>]
- [8] pry(main)>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement