So I am building a website for school that consists of different wikis that users can create, update, edit, etc. On my wiki edit page I allow an option for premium users to add or remove collaborators to a wiki that they have created if they choose to check their wiki as private.
I designed this add/remove collaborators feature to be used with AJAX so that a user can toggle back and forth with another user that they choose to add or remove as a collaborator without refreshing the page.
Before I implemented the friendly_id gem, my add/remove feature was working just fine. However, after implementing the friendly_id gem, I've noticed a new bug that had been created. Let me explain..
Whenever I click to add a user as a collaborator, things appear as if they have worked and I am able to toggle back and forth from adding and removing. However, the user that I am trying to add initially is not getting added as a collaborator, and it is apparent in the stack trace.
This is what happens after clicking the 'add' link for the first time:
Started POST "/wikis/hipsters/collaborators?user_id=3" for 127.0.0.1 at 2015-09-28 17:51:06 -0700
Processing by CollaboratorsController#create as JS
Parameters: {"user_id"=>"3", "wiki_id"=>"hipsters"}
Wiki Load (0.2ms) SELECT "wikis".* FROM "wikis" WHERE "wikis"."slug" = 'hipsters' ORDER BY "wikis"."id" ASC LIMIT 1
(0.1ms) begin transaction
SQL (0.4ms) INSERT INTO "collaborators" ("created_at", "updated_at", "user_id", "wiki_id") VALUES (?, ?, ?, ?) [["created_at", "2015-09-29 00:51:06.185794"], ["updated_at", "2015-09-29 00:51:06.185794"], ["user_id", 3], ["wiki_id", 0]]
(85.8ms) commit transaction
(0.1ms) begin transaction
(0.1ms) commit transaction
(0.2ms) SELECT COUNT(*) FROM "collaborators" WHERE "collaborators"."wiki_id" = ? [["wiki_id", 51]]
Rendered collaborators/create.js.haml (3.0ms)
Completed 200 OK in 198ms (Views: 106.9ms | ActiveRecord: 86.8ms)
Notice how a new collaborator is attempted to be made with a ["user_id", 3], ["wiki_id", 0]
The user_id is correct, but the wiki_id is not. Why is this happening? The friendly_id gem is working, as you can see the name of this particular wiki is "hipsters".
This is made apparent here:
Parameters: {"user_id"=>"3", "wiki_id"=>"hipsters"}
Wiki Load (0.2ms) SELECT "wikis".* FROM "wikis" WHERE "wikis"."slug" = 'hipsters' ORDER BY "wikis"."id" ASC LIMIT 1
However, since I just attempted to 'add' a user as a collaborator, now the link has toggled to read 'remove', to get rid of a collaborator.
Upon clicking to remove the collaborator, my server log displays the following:
Started DELETE "/wikis/51/collaborators/54" for 127.0.0.1 at 2015-09-28 18:02:39 -0700
Processing by CollaboratorsController#destroy as JS
Parameters: {"wiki_id"=>"51", "id"=>"54"}
Wiki Load (0.3ms) SELECT "wikis".* FROM "wikis" WHERE "wikis"."slug" = '51' ORDER BY "wikis"."id" ASC LIMIT 1
Wiki Load (0.1ms) SELECT "wikis".* FROM "wikis" WHERE "wikis"."id" = ? LIMIT 1 [["id", 51]]
Collaborator Load (0.1ms) SELECT "collaborators".* FROM "collaborators" WHERE "collaborators"."id" = ? LIMIT 1 [["id", 54]]
(0.1ms) begin transaction
SQL (0.4ms) DELETE FROM "collaborators" WHERE "collaborators"."id" = ? [["id", 54]]
(30.1ms) commit transaction
(0.2ms) SELECT COUNT(*) FROM "collaborators" WHERE "collaborators"."wiki_id" = ? [["wiki_id", 51]]
Rendered collaborators/destroy.js.haml (2.0ms)
Completed 200 OK in 52ms (Views: 16.1ms | ActiveRecord: 31.3ms)
Then, when I toggle to add the user as a collaborator again, this time they actually get added.
Server log:
Started POST "/wikis/51/collaborators?user_id=3" for 127.0.0.1 at 2015-09-28 18:05:59 -0700
Processing by CollaboratorsController#create as JS
Parameters: {"user_id"=>"3", "wiki_id"=>"51"}
Wiki Load (0.2ms) SELECT "wikis".* FROM "wikis" WHERE "wikis"."slug" = '51' ORDER BY "wikis"."id" ASC LIMIT 1
Wiki Load (0.1ms) SELECT "wikis".* FROM "wikis" WHERE "wikis"."id" = ? LIMIT 1 [["id", 51]]
(0.1ms) begin transaction
SQL (0.7ms) INSERT INTO "collaborators" ("created_at", "updated_at", "user_id", "wiki_id") VALUES (?, ?, ?, ?) [["created_at", "2015-09-29 01:05:59.564379"], ["updated_at", "2015-09-29 01:05:59.564379"], ["user_id", 3], ["wiki_id", 51]]
(182.8ms) commit transaction
(0.1ms) begin transaction
(0.1ms) commit transaction
(0.2ms) SELECT COUNT(*) FROM "collaborators" WHERE "collaborators"."wiki_id" = ? [["wiki_id", 51]]
Rendered collaborators/create.js.haml (3.1ms)
Completed 200 OK in 198ms (Views: 8.3ms | ActiveRecord: 184.2ms)
I notice that this time around the parameters read Parameters: {"user_id"=>"3", "wiki_id"=>"51"}
instead of Parameters: {"user_id"=>"3", "wiki_id"=>"hipsters"}
Why is this? How can I get this feature to function correctly?
Here's some relevant files:
wikis_controller.rb:
class WikisController < ApplicationController
def index
@wikis = policy_scope(Wiki)
end
def show
@wiki = Wiki.friendly.find(params[:id])
end
def new
@wiki = Wiki.new
authorize @wiki # Example
end
def create
@wiki = Wiki.new(wiki_params)
@wiki.user = current_user
authorize @wiki
if @wiki.save
flash[:notice] = "Wiki was saved."
redirect_to @wiki
else
flash[:error] = "There was an error saving the wiki. Please try again."
render :new
end
end
def edit
@wiki = Wiki.friendly.find(params[:id])
@users = User.all
authorize Wiki, :edit?
end
def update
@wiki = Wiki.friendly.find(params[:id])
authorize @wiki
if @wiki.update_attributes(wiki_params)
flash[:notice] = "Wiki was updated."
redirect_to @wiki
else
flash[:error] = "There was an error saving the wiki. Please try again."
render :edit
end
end
def destroy
@wiki = Wiki.friendly.find(params[:id])
authorize Wiki, :destroy?
if @wiki.destroy
flash[:notice] = "\"#{@wiki.title}\" was deleted successfully."
redirect_to wikis_path
else
flash[:error] = "There was an error deleting the wiki."
render :show
end
end
private
def wiki_params
params.require(:wiki).permit(:title, :body, :private)
end
end
collaborators_controller.rb:
class CollaboratorsController < ApplicationController
def create
@wiki = Wiki.friendly.find(params[:wiki_id])
@collaborator = Collaborator.create(wiki_id: params[:wiki_id], user_id: params[:user_id])
respond_to do |format|
format.html
format.js
end
end
def destroy
@wiki = Wiki.friendly.find(params[:wiki_id])
@collab = Collaborator.find(params[:id])
@user_id = @collab.user_id
@collab.destroy
respond_to do |format|
format.html
format.js
end
end
end
collaborators/create.js.haml:
- if @collaborator.save
$('#'+#{ params[:user_id] }).replaceWith('<a data-method="delete" data-remote="true" href="/wikis/'+#{ @wiki.id }+'/collaborators/'+#{ @collaborator.id }+'" id="#{ @collaborator.user_id }" rel="nofollow">Remove</a>');
$('.js-collaborators-count').html("Currently #{ pluralize(@wiki.collaborators.count, 'collaborator') } on this wiki.");
- else
$('#'+#{ params[:user_id] }).prepend("<div class='alert alert-danger'>#{ flash[:error] = 'Oops something went wrong..' }</div>");
collaborators/destroy.js.haml:
- if @collab.destroyed?
$('#'+#{ @user_id.to_s }).replaceWith('<a data-method="post" data-remote="true" href="/wikis/'+#{ @wiki.id }+'/collaborators?user_id=#{ @user_id }" id="#{ @user_id }" rel="nofollow">Add</a>');
$('.js-collaborators-count').html("Currently #{ pluralize(@wiki.collaborators.count, 'collaborator') } on this wiki.");
- else
$('#'+#{ @user_id.to_s }).prepend("<div class='alert alert-danger'>#{ flash[:error] = 'Oops something went wrong..' }</div>");
wikis/edit.html.haml:
%h1 Edit Wiki
.row
.col-md-8
= form_for @wiki do |f|
.form-group
= f.label :title
= f.text_field :title, class: 'form-control', placeholder: "Enter wiki title"
.form-group
= f.label :body
= f.text_area :body, rows: 8, class: 'form-control', placeholder: "Enter wiki body"
- if current_user.role == 'admin' || (current_user.role == 'premium' && @wiki.user == current_user)
= render 'wikis/form', f: f
- if current_user.role == 'admin' || (@wiki.private == true && @wiki.user == current_user)
.form-group
%h5
Add/Remove Collaborators to the
%em “#{ @wiki.title }”
wiki (Showing all users)
%small
.js-collaborators-count
Currently #{ pluralize(@wiki.collaborators.count, 'collaborator') } on this wiki.
.col-sm-4.text-center.outline
%h6.underline Name
- @users.each do |u|
.border_bottom
= u.name
%br/
.col-sm-4.text-center.outline
%h6.underline Email
- @users.each do |u|
.border_bottom
= u.email
.col-sm-4.text-center.outline
%h6.underline Give Access
- @users.each do |u|
.border_bottom
- if collab = u.collaborators.find_by(wiki_id: @wiki.id)
= link_to "Remove", wiki_collaborator_path(@wiki, collab), method: :delete, remote: true, id: u.id
- else
= link_to "Add", wiki_collaborators_path(@wiki, user_id: u.id), method: :post, remote: true, id: u.id
.form-group
= f.submit "Update", class: 'btn btn-success mt_10'
If I left any files out you can view my repo on github here: