每当我尝试创建新用户时,都会收到一条错误消息,提示“密码不能留空”,但在提交前密码表单中有内容。
以下是 application.html.erb 中的表单:
<div class="signup_form_div">
<h1>Create an account!</h1>
<%= form_for(:user, :url => {:controller => 'users', :action => 'create'}) do |f| %>
<p class="text_fields">
<%= f.text_field :first_name, placeholder: 'First Name' %>
<%= f.text_field :last_name, placeholder: 'Last Name' %>
<%= f.text_field :username, placeholder: 'Desired Username' %>
<%= f.text_field :email, placeholder: 'Email Address' %>
<%= f.password_field :password, placeholder: 'Password' %>
<%= f.password_field :password_confirmation, placeholder: 'Confirm Password' %>
</p>
<p id="signup_submit">
<%= f.submit :Signup, class: 'button'%>
</p>
<% end %>
</div>
这是 users_controller.rb 文件:
def new
@user = User.new
end
def create
@user = User.new( user_params )
logger.debug user_params
if @user.save
flash[:notice] = "You've signed up successfully"
flash[:color] = "valid"
else
flash[:notice] = "Form is invalid"
flash[:color] = "invalid"
end
render "new"
end
private
def user_params
params.require(:user).permit(:username, :email, :password, :password_confirmation, :first_name, :last_name)
end
,这是 user.rb 模型文件的顶部:
attr_accessor :password
#validation of form input
has_secure_password
EMAIL_REGEX = /\A[^@\s]+@([^@.\s]+\.)+[^@.\s]+\z/
validates :username, :presence => true, :uniqueness => true, :length => { :in => 3..20 }
before_save { :email.downcase }
validates :email, :presence => true, :uniqueness => { case_sensitive: false }, :format => EMAIL_REGEX
validates_length_of :password, :in => 6..20, :on => :create
试图创建名为“John Doe”的用户,用户“jdoe”,电子邮件“jdoe@email.com”和密码“password1”的日志:
Started POST "/create" for 127.0.0.1 at 2014-05-17 13:22:58 -0400
Processing by UsersController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"B+5wtiVRnM4kqFi5/MN8dTtSYkV+ppZXrM+7YekAMJw=", "user"=>{"first_name"=>"John", "last_name"=>"Doe", "username"=>"jdoe", "email"=>"jdoe1@email.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Signup"}
{"username"=>"jdoe", "email"=>"jdoe1@email.com", "password"=>"password1", "password_confirmation"=>"password1", "first_name"=>"John", "last_name"=>"Doe"}
[1m[35m (0.3ms)[0m BEGIN
[1m[36mUser Exists (0.4ms)[0m [1mSELECT 1 AS one FROM `users` WHERE `users`.`username` = BINARY 'jdoe' LIMIT 1[0m
[1m[35mUser Exists (0.2ms)[0m SELECT 1 AS one FROM `users` WHERE `users`.`email` = 'jdoe1@email.com' LIMIT 1
[1m[36m (0.1ms)[0m [1mROLLBACK[0m
Rendered users/new.html.erb within layouts/application (2.4ms)
Completed 200 OK in 72ms (Views: 9.4ms | ActiveRecord: 3.8ms)
如果有任何其他文件需要帮助,请告诉我。 感谢
答案 0 :(得分:0)
您需要在表单中引用@user
对象。
<%= form_for(@user) do |f| %>
注意:user
=&gt; @user
。根据{{1}}对象是新记录还是旧记录,rails还会知道是POST
还是PUT
,它可以通过调用{{1}上的@user
来执行此操作对象。
答案 1 :(得分:0)
除了需要@use
标记中的form_for
r(如上所示)外,表单本身也需要位于特定位置。
在你的问题中,你说的是application.html.erb中的表格,它不是正确的位置。
如果是新用户,则需要填写表单 应用程序/视图/用户/ new.html.erb
控制器应该在
app/controllers/users_controller.rb
(不是user_controller.rb)
你的config/routes.rb
resources :users
假设您正确设置了路由,那么当GET请求到达/ users / new时,它将被路由到users_controller.rb
的新方法,这将创建用户的新实例然后进行可用于new.html.erb
视图。