Rails API with devise, can't run POST #create method with JSON call from client -


so i'm trying create rails api devise. client side works get, can't post type of calls. if try create user keep getting following devise html form response.

<h2>sign up</h2>    <form class="new_user" id="new_user" action="/users" accept-charset="utf-8" method="post"><input name="utf8" type="hidden" value="&#x2713;" /><input type="hidden" name="authenticity_token" value="rgzxluxcocbxkvdpfc/wsakep3wcxqpfnvmv0nstecopppyviwcmdu+dpt84xqeskcmwrg0rezn+vtdx1j+moq==" />        <div id="error_explanation">        <h2>2 errors prohibited user being saved:</h2>        <ul><li>email can&#39;t blank</li><li>password can&#39;t blank</li></ul>      </div>        <div class="field">      <div class="field_with_errors"><label for="user_email">email</label></div><br />      <div class="field_with_errors"><input autofocus="autofocus" type="email" name="user[email]" id="user_email" /></div>    </div>      <div class="field">      <div class="field_with_errors"><label for="user_password">password</label></div>      <em>(4 characters minimum)</em>      <br />      <div class="field_with_errors"><input autocomplete="off" type="password" name="user[password]" id="user_password" /></div>    </div>      <div class="field">      <label for="user_password_confirmation">password confirmation</label><br />      <input autocomplete="off" type="password" name="user[password_confirmation]" id="user_password_confirmation" />    </div>      <div class="actions">      <input type="submit" name="commit" value="sign up" />    </div>  </form>    <a href="/users/sign_in">log in</a><br />          <a href="/users/confirmation/new">didn&#39;t receive confirmation instructions?</a><br />

interestingly, @ first "can't verify csrf token authenticity" error, added beforesend json call. change call options, though specified post.

my call:

$(document).on('page:change', function(){    $('body').on('submit', 'form.create_new_user', function(e){      e.preventdefault();      var user_data = $(this).serializejson();      $.ajax({        url: host.address + '/users',        beforesend: function(xhr) {xhr.setrequestheader('x-csrf-token', $('meta[name="csrf-token"]').attr('content'))},        type: 'post',        datatype:'json',        data: {info: {full_name: user_data.full_name,                      email: user_data.email,                      password: user_data.password,                      password_confirmation: user_data.password_confirmation}}      }).done(function(response){        console.log(response)        if (response.success.success) {          logingotowithnotice(response, '/', 'thank signig up');        } else{          alert("user wasn't created. please try again.")        };      });    });  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

and api this:

started options "/users" ::1 @ 2015-09-30 14:09:49 +0200  actioncontroller::routingerror (uninitialized constant registrationscontroller): ... 

if remove beforesend, i'd form again, "can't verify csrf token authenticity" error wouldn't there anymore, instead i'd successful post, still returns html form.

started post "/users" ::1 @ 2015-09-30 14:25:57 +0200    processing devise::registrationscontroller#create */* parameters: {"info"=>{"full_name"=>"user admin", "email"=>"user.admin@email.com", "password"=>"[filtered]", "password_confirmation"=>"[filtered]"}}   (0.1ms)  begin   (0.1ms)  rollback rendered /users/antonpot/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/devise-3.5.2/app/views/devise/shared/_links.html.erb (0.2ms) rendered /users/antonpot/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/devise-3.5.2/app/views/devise/registrations/new.html.erb (3.5ms)   completed 200 ok in 9ms (views: 4.7ms | activerecord: 0.2ms) 

now if users controller can see i'm never running #create, never prints p "c"*99

class users::registrationscontroller < devise::registrationscontroller   def create     p "c"*99     user = user.new     user.full_name = user_params[:full_name]     user.email = user_params[:email].downcase     user.password = user_params[:password]     user.password_confirmation = user_params[:password_confirmation]     if user.save       render json: {success:{success: true, user_id: user.id, errormessage: nil, errornumber: 201}}     else       render json: {success:{success: false, errormessage: user.errors.messages.to_s, errornumber: 400}}     end   end end 

why can't call it? need work?

first

this issue because of cross-site-scripting problem. client url different api url. http method options, pre-flight request browser makes determine whether cross-domain ajax request should allowed.

if cross-domain ajax request allowed post actual request sent.

so need allow cross-domain ajax request on server.

second

make sure routes.rb set correctly.

you need configure devise , instruct him use customized registration_controller instead of default devise registration controller

adjust devise_for :users in routes.rb following:

devise_for :users, controllers: { registrations: "users/registrations"} 

assuming customized registration class exist in users folder


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -