我正在尝试完成注册部分,但每次我尝试在站点注册时,服务器都会显示数据库已回滚的信息。我无法在db文件中找到新用户。
Started POST "/users" for 127.0.0.1 at 2014-03-12 17:09:50 +0800
Processing by UsersController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"CkaL2ZiNW8FWXRjPawShiNhQGmP+EHDMgSBbSyihE5E=", "user"=>{"name"=>"foo1", "email"=>"foo@foobar.org", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Create my account"}
(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/new.html.erb within layouts/application (5.2ms)
Completed 200 OK in 24ms (Views: 13.2ms | ActiveRecord: 0.5ms)
users_controller.rb:
class UsersController < ApplicationController
def new
@user = User.new
end
def index
end
def show
@user=User.find(params[:id])
end
def create
@user=User.new(params[:id])
if @user.save
#balabala
else
render 'new'
end
end
end
new.html.rb:
<% provide(:title, 'Sign up') %>
<h1>Sign up</h1>
<%= form_for(@user) do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.label :password_confirmation, "Confirmation" %>
<%= f.password_field :password_confirmation %>
<%= f.submit "Create my account", class: "btn btn-large btn-primary" %>
<% end %>
<span><%= params.inspect %></span>
<span><%= @user.errors.full_messages%></span>
路线:
resources :static_pages
resources :users
root to: 'static_pages#home'
match '/help', to: 'static_pages#help', via: 'get'
match '/signup', to: 'users#new', via:'get'
感谢。
答案 0 :(得分:1)
在创建操作中,替换:
User.new(params[:id])
使用:
User.new params.require(:user).permit(:name, :email, :password, :password_confirmation)
将数据传递给活动记录模型。
至于失败的原因:
有趣的事实:这是一个很好的例子,让数据库确保您有预期的数据(真的允许空电子邮件吗?)