Skip to content

Instantly share code, notes, and snippets.

@saitoxu
Created May 9, 2017 08:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saitoxu/7bb93564d4345ed95f61274e9b792b46 to your computer and use it in GitHub Desktop.
Save saitoxu/7bb93564d4345ed95f61274e9b792b46 to your computer and use it in GitHub Desktop.
2017-05-09
onSubmit(e) {
e.preventDefault()
const fd = new FormData()
fd.append('user[name]', this.state.name)
fd.append('user[email]', this.state.email)
fetch('http://example.com/users', {
method: 'POST',
body: fd
}).then(response =>
response.json()
).then((json) => {
// do something
}).catch(err =>
console.log(err)
)
}
const fb = new FormData()
fb.append('name', 'saitoxu')
fb.append('email', 'saitoxu@example.com')
class UsersController < ApplicationController
def create
user = User.new(user_params)
if user.save
render json: { status: 'success' }
else
render json: { status: 'failure' }
end
end
private
def user_params
params.require(:user).permit(:name, :email)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment