0

I am not looking for a solution where Rails handles the form submission using form_for or something like that.

This is my error:

Started POST "/users" for 127.0.0.1 at 2012-11-20 17:55:31 -0600
Processing by UsersController#create as JSON
  Parameters: {"name"=>"Name", "email"=>"[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}
   (0.1ms)  begin transaction
  User Exists (0.2ms)  SELECT 1 AS one FROM "users" WHERE "users"."email" IS NULL LIMIT 1
   (0.1ms)  rollback transaction
  Rendered users/_signup_js.html.erb (0.0ms)
  Rendered users/new.html.erb within layouts/application (1.3ms)
Completed 200 OK in 147ms (Views: 15.9ms | ActiveRecord: 37.5ms)

*This is my _signup_js partial:*

<script type="text/javascript">
$(document).ready(function(){
  $("#signup-button").click(function(){
    var name = $("#name").val(),
        email = $("#email").val(),
        password = $("#password").val(),
        password_confirmation = $("#password-confirmation").val();

    $.ajax({
        type: 'POST',
        url: '/users',
        data: {
            name: name,
            email: email,
            password: password,
            password_confirmation: password_confirmation
        },
        dataType: 'json'
    });
  });
});
</script>

This is my users controller:

class UsersController < ApplicationController
  def new
  end
  def create
    @user = User.new(params[:user])
    print @user
    if @user.save
      redirect_to @user
      puts "========="
      print @user 
      puts "========="
    else
      puts "not working"
      render 'new'
    end
  end

  def show
    @user = User.find(params[:id])
  end
  def index
    @users = User.all
  end
end

This is my User model:

# == Schema Information
#
# Table name: users
#
#  id              :integer          not null, primary key
#  name            :string(255)
#  email           :string(255)
#  created_at      :datetime         not null
#  updated_at      :datetime         not null
#  password_digest :string(255)
#

class User < ActiveRecord::Base
  attr_accessible :email, :name, :password, :password_confirmation
  has_secure_password

  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i

  validates :name, presence: true, length: {maximum: 25}
  validates :email, presence: true,format: {with: VALID_EMAIL_REGEX}, uniqueness: {case_sensitive: false}
  validates :password, presence: true, length: {minimum: 6}
  validates :password_confirmation, presence: true
end

These are my routes in config/routes.rb:

  resources :users

  root to: 'static_pages#home'

  match '/home',    to: 'static_pages#home'
  match '/about',   to: 'static_pages#about'
  match '/contact', to: 'static_pages#contact'


  match '/signup', to: 'users#new'
5
  • What exactly is your question? I don't see an error where you say there is an error.
    – 44maagnum
    Nov 21, 2012 at 1:39
  • WHERE "users"."email" IS NULL ??
    – Nich
    Nov 21, 2012 at 1:48
  • Thanks for replying. It's not saving the entry to the database.
    – helicopter
    Nov 21, 2012 at 1:49
  • When I remove validation, my terminal prints the following: Started POST "/users" for 127.0.0.1 at 2012-11-20 19:56:17 -0600 Processing by UsersController#create as JSON Parameters: {"name"=>"Mohd Irtefa", "email"=>"[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"} (0.1ms) begin transaction (0.1ms) rollback transaction Rendered users/_signup_js.html.erb (0.0ms) Rendered users/new.html.erb within layouts/application (1.3ms) Completed 200 OK in 51ms (Views: 15.3ms | ActiveRecord: 2.6ms) And, its not saving the user.
    – helicopter
    Nov 21, 2012 at 1:56
  • Why is it rolling back immediately? Sorry for the horrible format in my previous comment.
    – helicopter
    Nov 21, 2012 at 1:57

1 Answer 1

1

It sounds like your @user record is not valid - see what @user.errors is in the else of your conditional.

Not the answer you're looking for? Browse other questions tagged or ask your own question.