首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我在Rails中使用前端的JS创建注册表单。我收到以下错误

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

Stack Overflow用户
提问于 2012-11-21 00:00:18
回答 1查看 130关注 0票数 0

我并不是在寻找Rails使用form_for或诸如此类的方法处理表单提交的解决方案。

这是我的错误:

代码语言:javascript
复制
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部分:*

代码语言:javascript
复制
<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>

这是我的用户控制器:

代码语言:javascript
复制
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

这是我的用户模型:

代码语言:javascript
复制
# == 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

--这些是我的配置/routeres.rb:中的路由

代码语言:javascript
复制
  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'
EN

回答 1

Stack Overflow用户

发布于 2012-11-21 02:36:49

听起来您的@user记录是无效的-查看条件的其他部分中的@user.errors是什么。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13484307

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档