Ruby on rails 这是继续开发RubyonRails web应用程序的好方法吗?
这是继续开发RubyonRails web应用程序的好方法吗 以下是注册样本: 用户模型:user.rbRuby on rails 这是继续开发RubyonRails web应用程序的好方法吗?,ruby-on-rails,Ruby On Rails,这是继续开发RubyonRails web应用程序的好方法吗 以下是注册样本: 用户模型:user.rb class User < ActiveRecord::Base attr_accessible :email, :name, :password, :username validates_presence_of :email, :name, :password, :username end 正确使用rails就是要遵循其惯例 将init更改为new 更改注册以创建 使用资源:路由的
class User < ActiveRecord::Base
attr_accessible :email, :name, :password, :username
validates_presence_of :email, :name, :password, :username
end
正确使用rails就是要遵循其惯例
- 将init更改为new
- 更改注册以创建
- 使用资源:路由的用户
attr\u accessible
,并在控制器中创建一种处理质量分配的新方法。这将允许您“动态”更改哪些字段可以根据不同的用户或用户进行更新
到目前为止,更新您编写的代码并不困难。我已经着手做了关键的改变
Gemfile
gem 'strong_parameters'
用户模型:user.php
class User < ActiveRecord::Base
include ActiveModel::ForbiddenAttributesProtection
validates_presence_of :email, :name, :password, :username
end
class UsersController < ApplicationController
def new
@user = User.new
end
def create
@user = User.new(user_params)
if @user.save
redirect_to login_path, :notice => "You have signed up successfully. You may now login!"
else
render :new, :alert => "There was an problem creating your account"
end
end
private
def user_params
params.require(:user).permit(:email, :name, :password, :username)
end
end
注意:除了使用强参数之外,我还对users\u controller.php
做了一些更改
.valid?
和.save
,而是将if
语句更改为仅使用.save
。这是因为.save
将调用.valid?
并相应地返回falsecreate
操作的redirect_to
和render
调用。redirect\u to
调用是一个更重要的更改,因为它有助于防止双重表单提交表单_编写代码,并在新建
和编辑
操作中使用它。保留您的代码
另外,我注意到您没有在表单中使用
标记。我认为这一点非常重要,因为它改善了用户体验
注意:我已经更改了注册页文件名,以匹配我的users\u controller.php版本中的更改
注册页面:new.html.erb
<h1>Signup!!</h1>
<%= render :partial => "form" %>
<%= form_for @user do |f| %>
<%= f.label :name %>
<%= f.text_field :name %><br />
<%= f.label :email, "Email Address" %>
<%= f.email_field :email %><br />
<%= f.label :username %>
<%= f.text_field :username %><br />
<%= f.label :password %>
<%= f.password_field :password %><br />
<%= f.submit "Sign Up" %>
<% end %>
如果您对我发布的内容有任何疑问,请询问。我会尽力解释我能解释的一切。您有更尖锐的问题吗?谢谢,但是控制器名称是否应该与型号名称相同?我是说user.rb和controller users\u controller.rb。非常感谢。但是控制器名称是否应该与模型名称相同?我的意思是user.rb与controller users\u controller.rb一起,而不是signin\u controller.rb???@user1664409是的,因为controller提供的操作与用户
模型一起工作。new
和create
操作将向数据库添加新用户。请看一下Ryan Bate关于从头开始构建身份验证的屏幕广播。您将看到,他使用userscoontroller
进行注册,但他创建了一个SessionController
来处理实际登录。他这样做是因为控制器和动作名称相互解释。
class User < ActiveRecord::Base
include ActiveModel::ForbiddenAttributesProtection
validates_presence_of :email, :name, :password, :username
end
class UsersController < ApplicationController
def new
@user = User.new
end
def create
@user = User.new(user_params)
if @user.save
redirect_to login_path, :notice => "You have signed up successfully. You may now login!"
else
render :new, :alert => "There was an problem creating your account"
end
end
private
def user_params
params.require(:user).permit(:email, :name, :password, :username)
end
end
<h1>Signup!!</h1>
<%= render :partial => "form" %>
<%= form_for @user do |f| %>
<%= f.label :name %>
<%= f.text_field :name %><br />
<%= f.label :email, "Email Address" %>
<%= f.email_field :email %><br />
<%= f.label :username %>
<%= f.text_field :username %><br />
<%= f.label :password %>
<%= f.password_field :password %><br />
<%= f.submit "Sign Up" %>
<% end %>