Devise and Ajax

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” %>

<%= f.label :password, ‘password’ %>:
<%= f.password_field :password, :id => “password” %>

<%= f.label :password_confirmation, ‘password_confirmation’ %>:
<%= f.password_field :password_confirmation %>

<% 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.

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?

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

Resolved. I had to post to user model, which triggers registration
controller.

Can you show how you posted to the user model?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs