我有一个页面,允许管理员用户通过在表单中输入详细信息来注册新用户。
<%= stylesheet_link_tag "signup" %>
<%= form_for(@user) do |f| %>
<% if @user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2>
<ul>
<% @user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="signup">
<div class = "field">Username:
<%= f.text_field :username, :class => "username" %></div>
</br>
<div class = "field">Email:
<%= f.email_field :email, :class => "email" %></div>
</br>
<div class = "field">First Name:
<%= f.text_field :firstname, :class => "firstname" %></div>
</br>
<div class = "field">Last Name:
<%= f.text_field :lastname, :class => "lastname" %></div>
</br>
<div class="field">
<%= f.label :admin %><br />
<%= f.text_field :admin, :class => "admin" %>
</div>
<div class = "field">
<div class="actions">
<%= f.submit "Register New User", :class => "button" %>
</div>
</div>
</div>
<% end %>
这是来自 admin_controller 的 new 和 create 操作:
def create
@user = User.new(params[:user])
if @user.save
respond_to do |format|
format.html { redirect_to @user, notice: 'User was successfully created.' }
format.json { render json: @user, status: :created, location: @user }
end
else
respond_to do |format|
format.html { render action: "new" }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
def new
@user = User.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @user }
end
end
填写完表格,点击注册新用户,我回到主页,数据没有进入数据库。
编辑:点击注册新用户后的完整日志我可以看到日志中的数据:
Started POST "/users" for 127.0.0.1 at 2012-09-17 12:27:49 +0100
Processing by Devise::RegistrationsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Bhh2ELWIFW9bENXeQ3Yta+bJqD7Dx8os9zdvnN0m5sM=", "user"=>{"username"=>"test", "email"=>"test@test.com", "firstname"=>"test", "lastname"=>"test", "admin"=>"false"}, "commit"=>"Register New User"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
Redirected to http://localhost:3000/
Filter chain halted as :require_no_authentication rendered or redirected
Completed 302 Found in 2ms (ActiveRecord: 0.2ms)
Started GET "/" for 127.0.0.1 at 2012-09-17 12:27:49 +0100
Processing by ProjectsController#index as HTML
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
Project Load (0.2ms) SELECT "projects".* FROM "projects"
Rendered projects/index.html.erb within layouts/application (1.0ms)
Completed 200 OK in 5ms (Views: 4.0ms | ActiveRecord: 0.4ms)
我是 Rails 新手,所以在尝试提供帮助时请记住这一点。提前致谢