Closed zachrispoli closed 7 years ago
class ProjectsController < ApplicationController
before_action :set_project, only: [:show, :edit, :update, :destroy]
# GET /projects
# GET /projects.json
def index
@projects = Project.all
end
# GET /projects/1
# GET /projects/1.json
def show
@project = Project.find_by(id: params[:id])
end
# Given the name of the project, find the project
def project_by_name
long_name = params[:name].split('-')
@project = Project.find_by(name: long_name[1])
redirect_to @project
end
# GET /projects/new
def new
@project = Project.new
end
# GET /projects/1/edit
def edit
end
# POST /projects
# POST /projects.json
def create
@project = Project.new(project_params)
@project.username = current_student.username
respond_to do |format|
if @project.save
create_repository
format.html { redirect_to @project }
format.json { render :show, status: :created, location: @project }
else
format.html { render :new }
format.json { render json: @project.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /projects/1
# PATCH/PUT /projects/1.json
def update
respond_to do |format|
if @project.update(update_params)
update_repository
format.html { redirect_to @project }
format.json { render :show, status: :ok, location: @project }
else
format.html { render :edit }
format.json { render json: @project.errors, status: :unprocessable_entity }
end
end
end
# DELETE /projects/1
# DELETE /projects/1.json
def destroy
@project.destroy
respond_to do |format|
format.html { redirect_to projects_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_project
@project = Project.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def project_params
params.require(:project).permit(:name)
end
def update_params
params.require(:project).permit(:name).permit(:file)
end
def update_repository
StudentsController.update_repository(current_student.username, "#{current_student.username}-#{@project.name}", 'Commit Message', @project.file)
end
def create_repository
StudentsController.create_repository(current_student.username, "#{current_student.username}-#{@project.name}")
end
end
Still playing around with this ....
var jq = document.createElement('script');
jq.src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js";
document.getElementsByTagName('head')[0].appendChild(jq);
... give time for script to load, then type.
jQuery.noConflict();
jQuery.ajax({
url: '/projects/1',
type: 'PUT',
data: { _method:'PUT', project:"<html>Test Project</html>", file:"<html>Test Project</html>", name:"test name" },
success: function(data) {
alert('Load was performed.');
}
});
try ignoring the project, the project should already be there because you're sending to projects/1
Does rails give any warnings/error messages at all?
Let me check.
2016-08-29T22:15:24.936652+00:00 heroku[router]: at=info method=PUT path="/projects/1" host=githubs-clubhouse.herokuapp.com request_id=720cd65c-589f-4912-8509-2534b8ae0a0a fwd="128.237.222.158" dyno=web.1 connect=1ms service=8ms status=422 bytes=1823
2016-08-29T22:15:24.930613+00:00 app[web.1]: Started PUT "/projects/1" for 128.237.222.158 at 2016-08-29 22:15:24 +0000
2016-08-29T22:15:24.933445+00:00 app[web.1]: Processing by ProjectsController#update as */*
2016-08-29T22:15:24.933470+00:00 app[web.1]: Parameters: {"project"=>"<html>Test Project</html>", "file"=>"<html>Test Project</html>", "name"=>"test name", "id"=>"1"}
2016-08-29T22:15:24.933628+00:00 app[web.1]: Can't verify CSRF token authenticity
2016-08-29T22:15:24.933925+00:00 app[web.1]: Completed 422 Unprocessable Entity in 0ms (ActiveRecord: 0.0ms)
2016-08-29T22:15:24.935271+00:00 app[web.1]:
2016-08-29T22:15:24.935275+00:00 app[web.1]: ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken):
2016-08-29T22:15:24.935277+00:00 app[web.1]: vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.2/lib/action_controller/metal/request_forgery_protection.rb:181:in `handle_unverified_request'
...
OK, pushed, try now.
Huh, getting a 404 error now.
We should hop on appear.in soon, might be easier to debug!
Sure!
Ill be on in about a minute, pop on when you're ready :)
Ok, I'm on now
jQuery.ajax({
url: '/projects/1',
type: 'PUT',
data: { file:"<html>Test Project</html>", name:"test33" },
success: function(data) {
alert('Load was performed.');
}
});
Alright!
I might have fixed it now!
Can you test when you get a chance?
Sent a couple put requests, it took a while and then spat out an ERR_TOO_MANY_REDIRECTS
Anything in the logs?
Looks like it's working!
I think you just need to make the ajax event fire only once per click, it's continuously calling.
Sarahs-MBP:kidhub sarahguthals$ heroku logs --app githubs-clubhouse
2016-08-31T00:17:40.465544+00:00 app[web.1]: (1.7ms) BEGIN
2016-08-31T00:17:40.467951+00:00 app[web.1]: (1.4ms) COMMIT
2016-08-31T00:17:40.467981+00:00 app[web.1]: In projects controller, name: test33
2016-08-31T00:17:40.467985+00:00 app[web.1]: In projects controller, file: <html>Test Project</html>
2016-08-31T00:17:40.469714+00:00 app[web.1]: Student Load (1.5ms) SELECT "students".* FROM "students" WHERE "students"."username" = $1 LIMIT 1 [["username", "test"]]
2016-08-31T00:17:41.102178+00:00 app[web.1]: project name: test-test33
2016-08-31T00:17:41.102187+00:00 app[web.1]: project text: <html>Test Project</html>
2016-08-31T00:17:41.931303+00:00 heroku[router]: at=info method=PUT path="/projects/1" host=githubs-clubhouse.herokuapp.com request_id=534b27e7-e57b-4072-9864-b67826976c2d fwd="67.186.6.75" dyno=web.1 connect=2ms service=1468ms status=302 bytes=942
2016-08-31T00:17:41.920208+00:00 app[web.1]: #<Sawyer::Resource:0x007fea89321790>
2016-08-31T00:17:41.920722+00:00 app[web.1]: Redirected to https://githubs-clubhouse.herokuapp.com/projects/1
2016-08-31T00:17:41.920886+00:00 app[web.1]: Completed 302 Found in 1460ms (ActiveRecord: 6.1ms)
2016-08-31T00:17:41.993161+00:00 app[web.1]: Started PUT "/projects/1" for 67.186.6.75 at 2016-08-31 00:17:41 +0000
2016-08-31T00:17:41.995350+00:00 app[web.1]: Processing by ProjectsController#update as */*
2016-08-31T00:17:41.995375+00:00 app[web.1]: Parameters: {"file"=>"<html>Test Project</html>", "name"=>"test33", "id"=>"1"}
2016-08-31T00:17:41.997472+00:00 app[web.1]: Project Load (1.5ms) SELECT "projects".* FROM "projects" WHERE "projects"."id" = $1 LIMIT 1 [["id", 1]]
2016-08-31T00:17:41.999321+00:00 app[web.1]: (1.4ms) BEGIN
2016-08-31T00:17:42.001462+00:00 app[web.1]: (1.4ms) COMMIT
2016-08-31T00:17:42.001515+00:00 app[web.1]: In projects controller, name: test33
2016-08-31T00:17:42.001536+00:00 app[web.1]: In projects controller, file: <html>Test Project</html>
2016-08-31T00:17:42.003137+00:00 app[web.1]: Student Load (1.4ms) SELECT "students".* FROM "students" WHERE "students"."username" = $1 LIMIT 1 [["username", "test"]]
2016-08-31T00:17:42.645813+00:00 app[web.1]: project name: test-test33
2016-08-31T00:17:42.645830+00:00 app[web.1]: project text: <html>Test Project</html>
2016-08-31T00:17:43.425068+00:00 heroku[router]: at=info method=PUT path="/projects/1" host=githubs-clubhouse.herokuapp.com request_id=ffd7dab5-ccec-4823-851c-e26ba0406d46 fwd="67.186.6.75" dyno=web.1 connect=1ms service=1427ms status=302 bytes=942
2016-08-31T00:17:43.414362+00:00 app[web.1]: Completed 302 Found in 1419ms (ActiveRecord: 5.6ms)
2016-08-31T00:17:43.414148+00:00 app[web.1]: Redirected to https://githubs-clubhouse.herokuapp.com/projects/1
2016-08-31T00:17:43.541108+00:00 app[web.1]: Processing by ProjectsController#update as */*
2016-08-31T00:17:43.537268+00:00 app[web.1]: Started PUT "/projects/1" for 67.186.6.75 at 2016-08-31 00:17:43 +0000
2016-08-31T00:17:43.413417+00:00 app[web.1]: #<Sawyer::Resource:0x007fea88f01c28>
2016-08-31T00:17:43.541161+00:00 app[web.1]: Parameters: {"file"=>"<html>Test Project</html>", "name"=>"test33", "id"=>"1"}
2016-08-31T00:17:43.544079+00:00 app[web.1]: Project Load (1.6ms) SELECT "projects".* FROM "projects" WHERE "projects"."id" = $1 LIMIT 1 [["id", 1]]
2016-08-31T00:17:43.546491+00:00 app[web.1]: (1.5ms) BEGIN
2016-08-31T00:17:43.548936+00:00 app[web.1]: (1.5ms) COMMIT
2016-08-31T00:17:43.548986+00:00 app[web.1]: In projects controller, name: test33
2016-08-31T00:17:43.549002+00:00 app[web.1]: In projects controller, file: <html>Test Project</html>
2016-08-31T00:17:43.550947+00:00 app[web.1]: Student Load (1.6ms) SELECT "students".* FROM "students" WHERE "students"."username" = $1 LIMIT 1 [["username", "test"]]
2016-08-31T00:17:44.150734+00:00 app[web.1]: project name: test-test33
2016-08-31T00:17:44.150752+00:00 app[web.1]: project text: <html>Test Project</html>
2016-08-31T00:17:44.870158+00:00 heroku[router]: at=info method=PUT path="/projects/1" host=githubs-clubhouse.herokuapp.com request_id=cf5d93b2-8eba-4c8f-80fa-bc1e9861f7bf fwd="67.186.6.75" dyno=web.1 connect=1ms service=1331ms status=302 bytes=942
2016-08-31T00:17:44.859124+00:00 app[web.1]: #<Sawyer::Resource:0x007fea88a217a8>
2016-08-31T00:17:44.859679+00:00 app[web.1]: Redirected to https://githubs-clubhouse.herokuapp.com/projects/1
2016-08-31T00:17:44.859838+00:00 app[web.1]: Completed 302 Found in 1319ms (ActiveRecord: 6.1ms)
2016-08-31T00:17:44.965690+00:00 app[web.1]: Started PUT "/projects/1" for 67.186.6.75 at 2016-08-31 00:17:44 +0000
2016-08-31T00:17:44.968148+00:00 app[web.1]: Processing by ProjectsController#update as */*
2016-08-31T00:17:44.968167+00:00 app[web.1]: Parameters: {"file"=>"<html>Test Project</html>", "name"=>"test33", "id"=>"1"}
2016-08-31T00:17:44.970297+00:00 app[web.1]: Project Load (1.6ms) SELECT "projects".* FROM "projects" WHERE "projects"."id" = $1 LIMIT 1 [["id", 1]]
2016-08-31T00:17:44.974202+00:00 app[web.1]: (1.4ms) COMMIT
2016-08-31T00:17:44.972173+00:00 app[web.1]: (1.4ms) BEGIN
2016-08-31T00:17:44.974227+00:00 app[web.1]: In projects controller, name: test33
2016-08-31T00:17:44.974242+00:00 app[web.1]: In projects controller, file: <html>Test Project</html>
2016-08-31T00:17:44.975827+00:00 app[web.1]: Student Load (1.4ms) SELECT "students".* FROM "students" WHERE "students"."username" = $1 LIMIT 1 [["username", "test"]]
2016-08-31T00:17:45.585824+00:00 app[web.1]: project name: test-test33
2016-08-31T00:17:45.585856+00:00 app[web.1]: project text: <html>Test Project</html>
2016-08-31T00:17:46.399584+00:00 heroku[router]: at=info method=PUT path="/projects/1" host=githubs-clubhouse.herokuapp.com request_id=24f06610-5aeb-4c4a-9e93-11bfbc764dd4 fwd="67.186.6.75" dyno=web.1 connect=1ms service=1428ms status=302 bytes=942
2016-08-31T00:17:46.388627+00:00 app[web.1]: #<Sawyer::Resource:0x007fea87514c70>
2016-08-31T00:17:46.389099+00:00 app[web.1]: Redirected to https://githubs-clubhouse.herokuapp.com/projects/1
2016-08-31T00:17:46.389255+00:00 app[web.1]: Completed 302 Found in 1421ms (ActiveRecord: 5.7ms)
2016-08-31T00:17:46.425991+00:00 app[web.1]: Started PUT "/projects/1" for 67.186.6.75 at 2016-08-31 00:17:46 +0000
2016-08-31T00:17:46.429129+00:00 app[web.1]: Processing by ProjectsController#update as */*
2016-08-31T00:17:46.429218+00:00 app[web.1]: Parameters: {"file"=>"<html>Test Project</html>", "name"=>"test33", "id"=>"1"}
2016-08-31T00:17:46.431932+00:00 app[web.1]: Project Load (1.6ms) SELECT "projects".* FROM "projects" WHERE "projects"."id" = $1 LIMIT 1 [["id", 1]]
2016-08-31T00:17:46.433926+00:00 app[web.1]: (1.5ms) BEGIN
2016-08-31T00:17:46.436108+00:00 app[web.1]: (1.4ms) COMMIT
2016-08-31T00:17:46.436148+00:00 app[web.1]: In projects controller, name: test33
2016-08-31T00:17:46.436167+00:00 app[web.1]: In projects controller, file: <html>Test Project</html>
2016-08-31T00:17:46.437898+00:00 app[web.1]: Student Load (1.5ms) SELECT "students".* FROM "students" WHERE "students"."username" = $1 LIMIT 1 [["username", "test"]]
2016-08-31T00:17:47.077079+00:00 app[web.1]: project name: test-test33
2016-08-31T00:17:47.077096+00:00 app[web.1]: project text: <html>Test Project</html>
2016-08-31T00:17:47.890704+00:00 heroku[router]: at=info method=PUT path="/projects/1" host=githubs-clubhouse.herokuapp.com request_id=0f836e33-278c-43c3-8b8c-2a82b60ad05d fwd="67.186.6.75" dyno=web.1 connect=1ms service=1459ms status=302 bytes=942
2016-08-31T00:17:47.880035+00:00 app[web.1]: #<Sawyer::Resource:0x007fea865c70b0>
2016-08-31T00:17:47.880441+00:00 app[web.1]: Redirected to https://githubs-clubhouse.herokuapp.com/projects/1
2016-08-31T00:17:47.880588+00:00 app[web.1]: Completed 302 Found in 1451ms (ActiveRecord: 6.0ms)
2016-08-31T00:17:47.927045+00:00 app[web.1]: Started PUT "/projects/1" for 67.186.6.75 at 2016-08-31 00:17:47 +0000
2016-08-31T00:17:47.929559+00:00 app[web.1]: Processing by ProjectsController#update as */*
2016-08-31T00:17:47.929614+00:00 app[web.1]: Parameters: {"file"=>"<html>Test Project</html>", "name"=>"test33", "id"=>"1"}
2016-08-31T00:17:47.931744+00:00 app[web.1]: Project Load (1.5ms) SELECT "projects".* FROM "projects" WHERE "projects"."id" = $1 LIMIT 1 [["id", 1]]
2016-08-31T00:17:47.933635+00:00 app[web.1]: (1.4ms) BEGIN
2016-08-31T00:17:47.935714+00:00 app[web.1]: (1.4ms) COMMIT
2016-08-31T00:17:47.935753+00:00 app[web.1]: In projects controller, name: test33
2016-08-31T00:17:47.935766+00:00 app[web.1]: In projects controller, file: <html>Test Project</html>
2016-08-31T00:17:47.937383+00:00 app[web.1]: Student Load (1.5ms) SELECT "students".* FROM "students" WHERE "students"."username" = $1 LIMIT 1 [["username", "test"]]
2016-08-31T00:17:48.588492+00:00 app[web.1]: project name: test-test33
2016-08-31T00:17:48.588506+00:00 app[web.1]: project text: <html>Test Project</html>
2016-08-31T00:17:49.438897+00:00 heroku[router]: at=info method=PUT path="/projects/1" host=githubs-clubhouse.herokuapp.com request_id=490d3f02-d2d2-46ee-8430-a5ec9d108689 fwd="67.186.6.75" dyno=web.1 connect=1ms service=1507ms status=302 bytes=942
2016-08-31T00:17:49.427730+00:00 app[web.1]: #<Sawyer::Resource:0x007fea89665078>
2016-08-31T00:17:49.428257+00:00 app[web.1]: Redirected to https://githubs-clubhouse.herokuapp.com/projects/1
2016-08-31T00:17:49.428471+00:00 app[web.1]: Completed 302 Found in 1499ms (ActiveRecord: 5.7ms)
2016-08-31T00:17:49.576512+00:00 app[web.1]: Started PUT "/projects/1" for 67.186.6.75 at 2016-08-31 00:17:49 +0000
2016-08-31T00:17:49.580079+00:00 app[web.1]: Processing by ProjectsController#update as */*
2016-08-31T00:17:49.580450+00:00 app[web.1]: Parameters: {"file"=>"<html>Test Project</html>", "name"=>"test33", "id"=>"1"}
2016-08-31T00:17:49.583302+00:00 app[web.1]: Project Load (1.5ms) SELECT "projects".* FROM "projects" WHERE "projects"."id" = $1 LIMIT 1 [["id", 1]]
2016-08-31T00:17:49.585429+00:00 app[web.1]: (1.4ms) BEGIN
2016-08-31T00:17:49.588250+00:00 app[web.1]: (1.7ms) COMMIT
2016-08-31T00:17:49.588314+00:00 app[web.1]: In projects controller, name: test33
2016-08-31T00:17:49.588344+00:00 app[web.1]: In projects controller, file: <html>Test Project</html>
2016-08-31T00:17:49.590218+00:00 app[web.1]: Student Load (1.5ms) SELECT "students".* FROM "students" WHERE "students"."username" = $1 LIMIT 1 [["username", "test"]]
2016-08-31T00:17:50.161659+00:00 app[web.1]: project name: test-test33
2016-08-31T00:17:50.161676+00:00 app[web.1]: project text: <html>Test Project</html>
2016-08-31T00:17:51.025786+00:00 heroku[router]: at=info method=PUT path="/projects/1" host=githubs-clubhouse.herokuapp.com request_id=c95c9ece-a64f-479f-a90c-4dbfe9b87045 fwd="67.186.6.75" dyno=web.1 connect=1ms service=1444ms status=302 bytes=942
2016-08-31T00:17:51.014745+00:00 app[web.1]: #<Sawyer::Resource:0x007fea89311250>
2016-08-31T00:17:51.015279+00:00 app[web.1]: Redirected to https://githubs-clubhouse.herokuapp.com/projects/1
2016-08-31T00:17:51.015447+00:00 app[web.1]: Completed 302 Found in 1435ms (ActiveRecord: 6.2ms)
Huh, not sure why I'm sending out so many requests lol
It seems like the project is getting updated though!
It happened to me when I was testing earlier. Something about it bubbling up at every level so you have to "stop propagation"?
Ok! Here's some docs on how to get wick into "Clubhouse mode" and how to get it talking to the Rails instance:
If a user goes to the wick editor normally (for example, githubs-clubhouse.herokuapp.com/wick.htm
), the menu bar will have the usual open/export buttons:
However, if you supply Wick with a project variable in the URL (e.g. githubs-clubhouse.herokuapp.com?project=SomeProject
), then wick will send a get request to the rails instance to get the project named SomeProject
for the user who is currently authenticated. The menu bar will also change to look like this:
Where the GitHub button takes you back to the Clubhouse, and the save button sends a put request to overwrite SomeProject
.
Oh that's awesome!!!!!!!
I will try it out right now! :D
Thanks Zach!!!!
Sarah Guthals, PhD GitHub Independent Contractor Forbes 30 Under 30: Science sguthals@github.com
On Fri, Sep 2, 2016 at 12:56 PM, Zach Rispoli notifications@github.com wrote:
Ok! Here's how you can talk to wick to get it into Clubhouse mode and it can start sending requests to the Rails instance:
If a user goes to the wick editor normally (for example, githubs-clubhouse.herokuapp.com/wick.htm), the menu bar will have the usual open/export buttons:
[image: screen shot 2016-09-02 at 3 50 29 pm] https://cloud.githubusercontent.com/assets/4970417/18216527/49484834-7125-11e6-9614-32980beed7f6.png
However, if you supply Wick with a project variable in the URL (e.g. githubs-clubhouse.herokuapp.com?project=SomeProject), then wick will send a get request to the rails instance to get the project named SomeProject for the user who is currently authenticated. The menu bar will also change to look like this:
[image: screen shot 2016-09-02 at 3 50 38 pm] https://cloud.githubusercontent.com/assets/4970417/18216588/c003bc6a-7125-11e6-9c94-a3469187083f.png
Where the GitHub button takes you back to the Clubhouse, and the save button sends a put request to overwrite SomeProject.
— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/zrispo/wick/issues/117#issuecomment-244473102, or mute the thread https://github.com/notifications/unsubscribe-auth/ABQN7ZslvoVP-zKbHUd7f4xb2qdwoixrks5qmH9_gaJpZM4JuSCi .
Ok there may be an issue!
I'm not able to send PUT requests straight from wick.online, getting this error from rails:
We can either host the files for wick on githubs-clubhouse.herokuapp.com (which could be a problem as you would have to update the files every time there's an update to wick), or you might be able to change that header to accept requests from wick.online.
Let me know what would be easier for you!
I can add the Access-Control-Allow-Origin header to the projects controller. Trying that now.
OK, I've added the following: config.action_dispatch.default_headers = { 'Access-Control-Allow-Origin' => 'http://wick.online', 'Access-Control-Request-Method' => %w{GET POST OPTIONS}.join(",") }
I think that should allow you to Get, Post, and provide Options to my entire application.
Let me know if that works, or what errors you get.
Gives
422 Unprocessable Entity
. Mysterious!