Forum: Ruby on Rails Devise and Ajax

Posted by John Merlino (johnmerlino)
on 2011-03-07 00:41
Hey all,

I installed the devise plugin and I'm sure it's fully installed.

I have a popup where user creates an account, including email, password,
and password confirmation. When they click submit, I use jquery's .ajax
method:


          $.ajax({
              url: $('#dialog-form form:first').attr('action'),
              method: 'POST',
              data: $('#dialog-form form:first').serialize(),

              beforeSend: function(data){
                  $('#headerHome').html('Loading...');
                },
              success: function() { alert("success"); },
              failure: function() { alert("failure"); }
            });


That action attribute I inspect with firebug, and it's pointing to this
link:

/users

Although in the partial, I specify /users/create:

<%= form_for :user, :url => { :controller => "users", :action =>
"create" } do |f| %>
  <%= f.label :email, 'Email' %>:
  <%= f.text_field :email, :id => "email"  %><br />
  <%= f.label :password, 'password' %>:
  <%= f.password_field :password, :id => "password" %><br />
  <%= f.label :password_confirmation, 'password_confirmation' %>:
  <%= f.password_field :password_confirmation %><br />
<% end %>

The create method of my user's controller contains this:

 def create
    @user = User.new(params[:user])

    if @user.save
      respond_to do |format|
        format.json { render :json => @user.to_json, :status => 200 }
        format.xml  { head :ok }
        format.html { redirect_to :action => :index }
      end
    else
      respond_to do |format|
        format.json { render :text => "Could not create user", :status
=> :unprocessable_entity } # placeholder
        format.xml  { head :ok }
        format.html { render :action => :new, :status =>
:unprocessable_entity }
      end
    end
  end


Basically, all that happens when I click submit is a piece of html
renders on page "loading..." courtesy of the beforeSend callback
function. But it just hangs there. The success is never called and the
record is never written to the database.

I'm not sure what the problem could be.

Thanks for response.
Posted by John Merlino (johnmerlino)
on 2011-03-08 20:07
I actually now hard code the create method I want to invoke:

                    var options = {
                        type: 'POST',
                        url: "users/create",
                        dataType: 'json',
                        data: $('#sign-in form:first').serialize(),
                        error: function(xhr, textStatus, errorThrown) {
                            alert('An error occurred! ' + errorThrown);
                        },
                        success: function(data, textStatus) {
                            $('body').append( data );
                        }
                    };
                    $.ajax( options );

The server output is this:

Started POST "/users/create" for 127.0.0.1 at 2011-03-08 14:01:30 -0500

ActionController::RoutingError (No route matches "/users/create"):

The JavaScript allert triggers:
"An eror occurred! undefined"


I have this in routes:

   devise_for :user
  resources :users

Any idea what's going on?
Posted by John Merlino (johnmerlino)
on 2011-03-08 20:25
I modified the routes to point to controller. But now I get this:

Started POST "/users/create" for 127.0.0.1 at 2011-03-08 14:21:21 -0500
  Processing by UsersController#create as JSON
  SQL (177.7ms)  BEGIN
  SQL (1.6ms)  ROLLBACK
Rendered text template (0.0ms)
Completed 422 Unprocessable Entity in 290ms (Views: 47.3ms | 
ActiveRecord: 179.3ms)

this is create method:

  def create
    @user = User.new(params[:user])

    if @user.save
      respond_to do |format|
        format.json { render :json => @user.to_json, :status => 200 }
        format.xml  { head :ok }
        format.html { redirect_to :action => :index }
      end
    else
      respond_to do |format|
        format.json { render :text => "Could not create user", :status 
=> :unprocessable_entity } # placeholder
        format.xml  { head :ok }
        format.html { render :action => :new, :status => 
:unprocessable_entity }
      end
    end


    end
Posted by John Merlino (johnmerlino)
on 2011-03-08 21:06
Resolved. I had to post to user model, which triggers registration 
controller.
Posted by Michael M. (michael_m56)
on 2013-02-09 04:40
(Received via mailing list)
Can you show how you posted to the user model?
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.