Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
R
react-rails-rspec
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Aman Sharma
react-rails-rspec
Commits
2b292b16
Commit
2b292b16
authored
Oct 01, 2020
by
Vijay Gawariya
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
create upload file api
parent
3c26d308
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
63 additions
and
1 deletions
+63
-1
employees_controller.rb
app/controllers/api/v1/employees_controller.rb
+6
-0
NewEmployee.jsx
app/javascript/components/NewEmployee.jsx
+56
-1
routes.rb
config/routes.rb
+1
-0
No files found.
app/controllers/api/v1/employees_controller.rb
View file @
2b292b16
...
@@ -26,6 +26,12 @@ class Api::V1::EmployeesController < ApplicationController
...
@@ -26,6 +26,12 @@ class Api::V1::EmployeesController < ApplicationController
render
json:
{
message:
'Employee deleted!'
}
render
json:
{
message:
'Employee deleted!'
}
end
end
def
uploadFile
# print{'aaaaaaaaaaaaaaaa'}
print
(
params
.
permit
(
:file
),
'aaaaaaaaaaaaaaaaa'
)
render
json:
{
name:
'vijay'
}
end
private
private
def
employee_params
def
employee_params
...
...
app/javascript/components/NewEmployee.jsx
View file @
2b292b16
...
@@ -10,11 +10,14 @@ class NewEmployee extends React.Component {
...
@@ -10,11 +10,14 @@ class NewEmployee extends React.Component {
email
:
""
,
email
:
""
,
phone
:
""
,
phone
:
""
,
dob
:
""
,
dob
:
""
,
address
:
""
address
:
""
,
file
:
{}
};
};
this
.
onChange
=
this
.
onChange
.
bind
(
this
);
this
.
onChange
=
this
.
onChange
.
bind
(
this
);
this
.
uploadFile
=
this
.
uploadFile
.
bind
(
this
);
this
.
onSubmit
=
this
.
onSubmit
.
bind
(
this
);
this
.
onSubmit
=
this
.
onSubmit
.
bind
(
this
);
this
.
submitFile
=
this
.
submitFile
.
bind
(
this
)
this
.
stripHtmlEntities
=
this
.
stripHtmlEntities
.
bind
(
this
);
this
.
stripHtmlEntities
=
this
.
stripHtmlEntities
.
bind
(
this
);
}
}
...
@@ -29,6 +32,43 @@ class NewEmployee extends React.Component {
...
@@ -29,6 +32,43 @@ class NewEmployee extends React.Component {
this
.
setState
({
[
event
.
target
.
name
]:
event
.
target
.
value
});
this
.
setState
({
[
event
.
target
.
name
]:
event
.
target
.
value
});
}
}
uploadFile
(
event
)
{
this
.
setState
({
[
event
.
target
.
name
]:
event
.
target
.
files
[
0
]
});
}
submitFile
(
event
)
{
event
.
preventDefault
();
const
url
=
"/api/v1/employees/uploadFile"
;
const
{
file
}
=
this
.
state
;
if
(
file
.
length
==
0
)
return
;
const
body
=
{
file
};
console
.
log
(
body
);
const
token
=
document
.
querySelector
(
'meta[name="csrf-token"]'
).
content
;
const
formData
=
new
FormData
()
formData
.
append
(
"file"
,
file
)
fetch
(
url
,
{
method
:
"POST"
,
headers
:
{
"X-CSRF-Token"
:
token
,
// "Content-Type": "application/json"
},
body
:
formData
})
.
then
(
response
=>
{
if
(
response
.
ok
)
{
alert
(
"AAAAAAAAAAA"
)
return
response
.
json
();
}
throw
new
Error
(
"Network response was not ok."
);
})
.
catch
(
error
=>
console
.
log
(
error
.
message
));
}
onSubmit
(
event
)
{
onSubmit
(
event
)
{
event
.
preventDefault
();
event
.
preventDefault
();
const
url
=
"/api/v1/employees/create"
;
const
url
=
"/api/v1/employees/create"
;
...
@@ -72,6 +112,21 @@ class NewEmployee extends React.Component {
...
@@ -72,6 +112,21 @@ class NewEmployee extends React.Component {
<
h1
className=
"font-weight-normal mb-5"
>
<
h1
className=
"font-weight-normal mb-5"
>
Add New Employee Details
Add New Employee Details
</
h1
>
</
h1
>
<
form
onSubmit=
{
this
.
submitFile
}
>
<
div
className=
"form-group"
>
<
label
htmlFor=
"file"
>
Upload File
</
label
>
<
input
type=
"file"
name=
"file"
className=
"form-control"
required
onChange=
{
this
.
uploadFile
}
/>
</
div
>
<
button
type=
"submit"
className=
"btn custom-button mt-3"
>
Submit File
</
button
>
</
form
>
<
form
onSubmit=
{
this
.
onSubmit
}
>
<
form
onSubmit=
{
this
.
onSubmit
}
>
<
div
className=
"form-group"
>
<
div
className=
"form-group"
>
<
label
htmlFor=
"employeeFirstName"
>
First Name
</
label
>
<
label
htmlFor=
"employeeFirstName"
>
First Name
</
label
>
...
...
config/routes.rb
View file @
2b292b16
...
@@ -5,6 +5,7 @@ Rails.application.routes.draw do
...
@@ -5,6 +5,7 @@ Rails.application.routes.draw do
post
'employees/create'
post
'employees/create'
get
'/show/:id'
,
to:
'employees#show'
get
'/show/:id'
,
to:
'employees#show'
delete
'/destroy/:id'
,
to:
'employees#destroy'
delete
'/destroy/:id'
,
to:
'employees#destroy'
post
'employees/uploadFile'
end
end
end
end
namespace
:api
do
namespace
:api
do
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment