我正在使用Rails创建待办事项列表。因此我制作了一个用户模型(电子邮件和密码)。我希望用户能够在那里编辑帐户,这样我就可以在用户控制器上进行编辑和更新操作。我还有一个用户模型。一切都很好,但我的问题是,在设置视图中,用于更新电子邮件的text_field已经预先填入了模型中当前的电子邮件地址。我想禁用预填充因为设计方面(占位符的内容应该可以立即读取)。我知道当我解决这个问题时,用户模型会出现另一个问题。例如,如果用户只想更改密码,则用户将收到错误消息,例如:“电子邮件不能为空”和“电子邮件无效”...我能够修改密码的最小长度验证如果密码为空,因为我知道“has_secure_password”在对象创建时强制执行在线验证。所以我知道新用户无法使用空白密码注册。但是,如何为电子邮件验证创建相同的效果?我还没有找到解决方案,如果有人能帮助我,我将非常感激。提前谢谢!
问题:
users_controller.rb(控制器)
def edit
@user = User.find(params[:id])
end
def update
@user = User.find(params[:id])
if @user.update_attributes(user_params)
flash[:success] = "Profile updated!"
redirect_to @user
else
render 'edit'
end
end
edit.html.erb(查看)
<% provide(:title, "Settings") %>
<h4>SETTINGS</h4>
<h5>User Edit</h5>
<%= @user.email %>
<div class="settings">
<%= form_for(@user) do |f| %>
<%= render 'layouts/error_messages' %>
<p>Change Email:</p>
<%= f.text_field :email, placeholder: "New Email", class: "formfield" %>
<p>Change Password:</p>
<%= f.password_field :password, placeholder: "New Password", class: "formfield" %>
<%= f.password_field :password_confirmation, placeholder: "New Password Confirmation", class: "formfield" %>
<%= f.submit "Save Changes", class: "form_button" %>
<% end %>
<%= link_to "Delete my Account", '#' %>
</div>
user.rb(型号)
class User < ActiveRecord::Base
before_save { self.email = email.downcase }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
has_secure_password
validates :password, length: { minimum: 6 }, allow_blank: true
# Returns the hash digest of the given string.
def User.digest(string)
cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
BCrypt::Engine.cost
BCrypt::Password.create(string, cost: cost)
end
end
答案 0 :(得分:1)
如何在Rails中禁用预填表单?
为要禁用预填充的public class TailerTest
{
public static void main(String[] args)
{
File f = new File("/tmp/test.txt");
MyListener listener = new MyListener();
Tailer.create(f, listener, 2500);
try
{
FileOutputStream fos = new FileOutputStream(f);
int i = 0;
while (i < 200)
{
fos.write(("test" + ++i + "\n").getBytes());
Thread.sleep(150);
}
fos.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
private static class MyListener extends TailerListenerAdapter
{
@Override
public void handle(String line)
{
System.out.println(line);
}
}
}
添加value: nil
即可。
例如,
field
应修改哪些内容以接受设置表单中的空白电子邮件 没有将电子邮件更新为数据库中的空白?
在 电子邮件验证 中删除<%= f.text_field :email, value: nil, placeholder: "New Email", class: "formfield" %>
。
presence: true
答案 1 :(得分:0)
<强>更新强>
您必须在编辑表单中首次使用value=nil
添加属性,以便显示占位符。
<%= f.text_field :email, value: nil, placeholder: "New Email", class: "formfield" %>
然后,为了避免模型中的验证,您可以在验证之前在控制器中添加值电子邮件。例如:
def update
@user = User.find(params[:id])
user_params[:email] = @user.email if params[:user][:email].nil?
if @user.update_attributes(user_params)
flash[:success] = "Profile updated!"
redirect_to @user
else
render 'edit'
end
end
我希望它可以帮到你
答案 2 :(得分:0)
只需使用form_tag,而不是form_for
喜欢这样&lt;%= form_tag'/ some_url'do%&gt; http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html
要在验证中添加空白电子邮件,请添加allow_blank:true
validates :email, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false },
allow_blank: true
http://edgeguides.rubyonrails.org/active_record_validations.html#allow-blank 验证presence和allow_blank是没有意义的。
答案 3 :(得分:0)
1 - 也许不是您正在寻找的解决方案,但您可以使用javascript清除该字段。
2 - 您可以使用rails docs:
中的条件验证class Order < ActiveRecord::Base
validates :email, presence: true, unless: :email_present?
def email_present?
self.email.blank?
end
end
代码未经过测试,但必须接近正常工作的解决方案。