Ruby on rails 我正在使用RubyonRails在前端使用JS制作注册表单。我得到以下错误
我不是在寻找Rails使用form_for或类似的方式处理表单提交的解决方案 这是我的错误: *这是我的注册:Ruby on rails 我正在使用RubyonRails在前端使用JS制作注册表单。我得到以下错误,ruby-on-rails,ruby-on-rails-3,ruby-on-rails-3.2,Ruby On Rails,Ruby On Rails 3,Ruby On Rails 3.2,我不是在寻找Rails使用form_for或类似的方式处理表单提交的解决方案 这是我的错误: *这是我的注册: <script type="text/javascript"> $(document).ready(function(){ $("#signup-button").click(function(){ var name = $("#name").val(), email = $("#email").val(), password =
<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>
听起来您的
@user
记录无效-请查看您的条件语句的else中的@user.errors
是什么。您的问题到底是什么?我看不出你说有错误的地方有错误。其中“用户”,“电子邮件”是空的??谢谢回复。它没有将条目保存到数据库中。当我删除验证时,我的终端打印以下内容:2012-11-20 19:56:17-0600 UsersController处理127.0.0.1的Started POST“/users”#创建为JSON参数:{“name”=>“Mohd Irtefa”,“email”=>irtefa1@illinois.edu“,“密码”=>“[过滤]”,“密码确认”=>“[过滤]”}(0.1ms)开始事务(0.1ms)回滚事务呈现用户/_signup_js.html.erb(0.0ms)呈现用户/new.html.erb在布局/应用程序中(1.3ms)在51ms内完成200 OK(视图:15.3ms;活动记录:2.6ms),并且它没有保存用户。为什么它会立即回滚?很抱歉我之前评论的格式太糟糕了。
<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
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'