我在前端使用JS在Ruby on Rails中创建注册表单。我收到以下错误

时间:2012-11-21 00:00:18

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.2

我不是在寻找Rails使用form_for处理表单提交的解决方案。

这是我的错误:

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"=>"name@email.com", "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)

* 这是我的_signup_js部分:*

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

这是我的用户控制器:

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

这是我的用户模型:

# == 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

这些是我在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'

1 个答案:

答案 0 :(得分:1)

听起来您的@user记录无效 - 请参阅条件的其他内容@user.errors