enter image description hereI am new to ruby on rails. I am trying to add data to database using ajax post request. But I am not able to do so. So, can anybody help me to add data to PostgreSQL database.
<script>
$(document).ready(function () {
$('#submit-button').click(function() {
$.ajax({
url: "http://localhost:3000/batches",
data: JSON.stringify({
name: $("#name").val(),
course_id: $("#course_id").val(),
start_date: $("#start_date").val(),
end_date: $("#end_date").val(),
status: $("#batch_status").val(),
}
),
error: function(error) {
console.log(error);
},
success: function(data) {
console.log(data);
},
type: 'POST'
});
});
})
</script>
And my code for controller is:
def new
@batch = Batch.new
respond_to do |format|
format.html
format.json
end
end
def create
@batch = Batch.new(batch_param)
respond_to do |format|
if @batch.save
format.html { redirect_to @batch, notice: "Save process completed!" }
format.json { render json: @batch, status: :created, location: @batch }
else
format.html {
flash.now[:notice]="Save proccess coudn't be completed!"
render :new
}
format.json { render json: @batch.errors, status: :unprocessable_entity}
end
end
end
def batch_param
params.require(:batch).permit(:name, :course_id, :start_date, :end_date, :status)
end
And also let me know what am I doing wrong here. Or should I also have to add extra code for better operation. What changes should be done on routes and model for this operation?
These are the errors i have been getting...