Exploring the world of Rails and diving into creating a basic rails-api. Currently facing an issue while trying to incorporate user addition to my model using a JavaScript request...
Let's take a look at my HTML file named add-user.html:
<script type="text/javascript" charset="utf-8">
$(function () {
$('#adduser').submit(function(e){
$.post('http://localhost:3000/users', {user: {username: $("#usr").value}, user: {password:$("#psw").value}});
});
});
</script>
<form id="adduser" data-ajax="false">
<input type="text" id="usr" placeholder="Username"/>
<input type="password" id="psw" placeholder="Password"/>
<input type="submit" value="Add User" id="usradd" name="login"/>
</form>
Upon clicking submit, I noticed that $.post() simply appends the data to my URL rather than including it in my model...
Here's a snippet from my users_controller code:
def new
@user = User.new
render json: @user
end
def create
@user = User.new(params[:user])
if @user.save
render json: @user, status: :created, location: @user
else
render json: @user.errors, status: :unprocessable_entity
end
end