This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import './App.css'; | |
import Uploader from './uploader'; | |
const fetchTestDatas = function () { | |
fetch('/api/v1/tests') | |
.then(res => res.json()) | |
.then((response) => { console.log("Test datas response", response); }) | |
.catch((error) => { console.log("Error while fetching test datas", error); }) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react' | |
import Axios from 'axios' | |
const submitForm = function (event) { | |
event.preventDefault(); | |
let formData = new FormData(event.target); | |
Axios.post('/api/v1/uploads', formData) | |
.then(res => { | |
alert(`File has been uploaded ${res.data.url}`) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react' | |
import Axios from 'axios' | |
const submitForm = function (event) { | |
event.preventDefault(); | |
let formData = new FormData(event.target); | |
console.log(formData.get('upload[file]')) | |
Axios.post('/api/v1/uploads', formData) | |
.then(res => { | |
console.log(res) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Api::V1::UploadsController < ApplicationController | |
def create | |
@upload = Upload.new(upload_params) | |
if @upload.save | |
render json: {upload: @upload} | |
else | |
render json: {error: @upload.errors.full_messages}, status: 422 | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Upload < ApplicationRecord | |
has_one_attached :file | |
after_create :build_url | |
private | |
def build_url | |
update_column(:url, Rails.application.routes.url_helpers.rails_blob_path(file, only_path: true)) if file.attached? | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Upload < ApplicationRecord | |
has_one_attached :file | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
web: cd client && yarn start | |
api: bundle exec rails s -p 3000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ApplicationController < ActionController::API | |
def frontend_index_html | |
render file: 'public/index.html' | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Rails.application.routes.draw do | |
namespace :api do | |
namespace :v1 do | |
get 'tests', to: 'tests#index' | |
end | |
end | |
get '*path', to: 'application#frontend_index_html', constraints: lambda { |request| | |
!request.xhr? && request.format.html? | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "client", | |
"version": "0.1.0", | |
"proxy": "http://127.0.0.1:3000", | |
"private": true, | |
"dependencies": { | |
"react": "^16.9.0", | |
"react-dom": "^16.9.0", | |
"react-scripts": "3.1.1" | |
}, |
NewerOlder