Advertisement
Guest User

Untitled

a guest
Mar 31st, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.58 KB | None | 0 0
  1. CollaboratorsController.rb
  2. ============================
  3. class CollaboratorsController < ApplicationController
  4. before_action :authenticate_user!
  5.  
  6. def create
  7. wiki = Wiki.find(params[:wiki_id])
  8. collaborator = current_user.collaborators.build(wiki: wiki)
  9.  
  10. if collaborator.save
  11. flash[:notice] = "Collaborator created"
  12. else
  13. flash[:alert] = "Collaborator creation failed."
  14. end
  15. redirect_to wikis_path
  16. end
  17.  
  18. def destroy
  19. wiki = Wiki.find(params[:id])
  20. collaborator = current_user.collaborators.where(wiki: wiki).create
  21.  
  22. if collaborator.destroy
  23. flash[:notice] = "Removed collaborator."
  24. else
  25. flash[:alert] = "Collaborator removal failed."
  26. end
  27. redirect_to wikis_path
  28. end
  29. end
  30.  
  31. _collaborator.html.erb
  32. ==========================
  33. <div class="form-group">
  34. <%# 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 %>
  35. <% User.all.each do |user| %>
  36. <% #if collaborator = user.collaborator_for(wiki) %>
  37. <% if wiki.private? && user != wiki.user && !wiki.collaborators.include?(user) %>
  38. <div class="user">
  39. <%= user.name %>
  40. <%= link_to "make collaborator", collaborators_path(wiki_id: wiki.id), class: 'btn btn-primary', method: :post %>
  41. </div>
  42. <% end %>
  43. <% if wiki.private? && user != wiki.user && wiki.collaborators.include?(user) %>
  44. <%= user.name %>
  45. <%= link_to "Remove_collaborator", collaborator_path(wiki), class: 'btn btn-danger', method: :delete %>
  46. <%#= link_to "delete collaborator", [wiki, collaborator], method: :delete do %>
  47. <% end %>
  48. <% end %>
  49. </div>
  50.  
  51. rails console output:
  52. =======================
  53. sharadalt:~/workspace (wiki_collaborators) $ rails c
  54. [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.
  55. Loading development environment (Rails 4.2.4)
  56. [1] pry(main)> wiki = Wiki.last
  57. Wiki Load (0.3ms) SELECT "wikis".* FROM "wikis" ORDER BY "wikis"."id" DESC LIMIT 1
  58. => #<Wiki:0x00000005dbb5f0
  59. id: 11,
  60. title: "wiki user2",
  61. body: "bbbbbbbb",
  62. private: true,
  63. user_id: 2,
  64. created_at: Wed, 30 Mar 2016 06:16:22 UTC +00:00,
  65. updated_at: Wed, 30 Mar 2016 06:16:22 UTC +00:00>
  66. [2] pry(main)> wiki.user
  67. User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
  68. => #<User:0x00000005d366c0
  69. id: 2,
  70. name: "user2",
  71. email: "user2@example.com",
  72. encrypted_password: "$2a$10$qREytQJfrjMp.O4dvuHaMuARBs4ZOdCyM/1yjxUonGneEz8NjobOW",
  73. reset_password_token: nil,
  74. reset_password_sent_at: nil,
  75. remember_created_at: nil,
  76. sign_in_count: 1,
  77. current_sign_in_at: Wed, 30 Mar 2016 06:15:05 UTC +00:00,
  78. last_sign_in_at: Wed, 30 Mar 2016 06:15:05 UTC +00:00,
  79. current_sign_in_ip: "69.181.65.0",
  80. last_sign_in_ip: "69.181.65.0",
  81. confirmation_token: "KogTgcBwNGGcDfhyDWuz",
  82. confirmed_at: Wed, 30 Mar 2016 05:54:19 UTC +00:00,
  83. confirmation_sent_at: Wed, 30 Mar 2016 05:54:18 UTC +00:00,
  84. unconfirmed_email: nil,
  85. failed_attempts: 0,
  86. [3] pry(main)>
  87. [4] pry(main)> wiki.collaborators
  88. Collaborator Load (0.3ms) SELECT "collaborators".* FROM "collaborators" WHERE "collaborators"."wiki_id" = ? [["wiki_id", 11]]
  89. => [#<Collaborator:0x00000005a842d8
  90. id: 1,
  91. user_id: 2,
  92. wiki_id: 11,
  93. created_at: Wed, 30 Mar 2016 06:16:31 UTC +00:00,
  94. updated_at: Wed, 30 Mar 2016 06:16:31 UTC +00:00>]
  95. [5] pry(main)> current_user
  96. NameError: undefined local variable or method `current_user' for main:Object
  97. from (pry):4:in `__pry__'
  98. [6] pry(main)> wiki.user
  99. => #<User:0x00000005d366c0
  100. id: 2,
  101. name: "user2",
  102. email: "user2@example.com",
  103. encrypted_password: "$2a$10$qREytQJfrjMp.O4dvuHaMuARBs4ZOdCyM/1yjxUonGneEz8NjobOW",
  104. reset_password_token: nil,
  105. reset_password_sent_at: nil,
  106. remember_created_at: nil,
  107. sign_in_count: 1,
  108. current_sign_in_at: Wed, 30 Mar 2016 06:15:05 UTC +00:00,
  109. last_sign_in_at: Wed, 30 Mar 2016 06:15:05 UTC +00:00,
  110. current_sign_in_ip: "69.181.65.0",
  111. last_sign_in_ip: "69.181.65.0",
  112. [7] pry(main)> wiki.user.collaborators
  113. Collaborator Load (0.3ms) SELECT "collaborators".* FROM "collaborators" WHERE "collaborators"."user_id" = ? [["user_id", 2]]
  114. => [#<Collaborator:0x00000005904fc0
  115. id: 1,
  116. user_id: 2,
  117. wiki_id: 11,
  118. created_at: Wed, 30 Mar 2016 06:16:31 UTC +00:00,
  119. updated_at: Wed, 30 Mar 2016 06:16:31 UTC +00:00>]
  120. [8] pry(main)>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement