0

我不是在寻找 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'
4

1 回答 1

1

听起来您的@user记录无效-查看@user.errors条件的 else 中的内容。

于 2012-11-21T02:36:49.660 回答