javascript - Making JS request to my Rails API -
i new rails. creating basic rails-api. trying add user model using js request ...
here html file: (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>
when click on submit, $.post() adds data url ... data not added model ...
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
actually dont need use $.post can submit form specifying action url in form tag itself. please check following.
<form action="/users/create" method="post"> <input type="text" id="usr" placeholder="username"/> <input type="password" id="psw" placeholder="password"/> <input type="button" value="add user" id="usradd" name="login"/> </form>
Comments
Post a Comment