如何在 Rails 3 中仅更新用户配置文件的单个字段?
我正在改编 Michael Hartl 的 Rails 3 教程中的身份验证系统,以便在(很可能)切换到 authlogic 或 devise 等系统之前更好地了解 Rails。我的身份验证系统运行良好,并且我有一个用户模型,现在包含构建用户配置文件的字段。例如,用户可以写一些关于自己的简介,例如“网球运动员、攀岩运动员”。
我想做的是能够有一个简单的表单,仅更新该特定字段(“blurb”)。我已经在用户个人资料页面上进行了设置,其中用户单击名为“编辑”的链接,这会导致部分呈现,显示带有字段的表单以更新用户简介,如下所示:
_edit_blurb.html.erb 中:
<div class = "editforms">
<%= form_for current_user do |f| %>
<div class="field">
<%= f.text_field :blurb, :class => "inputform round", :placeholder => "blurb" %>
</div>
<div class="actions">
<%= f.submit "Update", :class => "submit_button round mini_button" %> <br>
</div>
<% end %>
</div>
在 表单提交给用户控制器中的更新操作,并且在大多数情况下更新用户简介,而不更新任何其他内容。然而,一个问题是此更新操作提交了一个零密码(因为表单中没有密码字段) - 导致用户密码发生更改。由于显而易见的原因,这是有问题的。最初,我添加了一行:if => lamdba {新记录? || !密码.nil? } 到密码验证,以允许从 edit_blurb 部分进行更新而无需密码。但是,正如您所看到的,这允许保存零密码。
validates :password, :presence => true,
:if => lambda{ new_record? || !password.nil? },
:confirmation => true,
:length => { :within => 6..40 }
如何更改表单或验证(或两者),以便用户可以更新简介而无需更新/输入其密码(并修复重置为零的密码更新问题)?
我想我已经提供了相关的代码,但如果有帮助的话我会添加更多!谢谢大家!
I'm adapting the authentication system from Rails 3 Tutorial by Michael Hartl to better learn about rails before (most likely) switching to a system like authlogic or devise. My authentication system works great, and I have a user model that now includes fields that build a user profile. For example, users can write a blurb about themselves such as "tennis player, rock climber".
What I'd like to do is to be able to have a simple form that updates only that specific field ("blurb"). I've set this up on a user profile page where the user clicks a link called "edit" and this causes a partial to render showing a form with a field to update a user blurb as such:
in _edit_blurb.html.erb:
<div class = "editforms">
<%= form_for current_user do |f| %>
<div class="field">
<%= f.text_field :blurb, :class => "inputform round", :placeholder => "blurb" %>
</div>
<div class="actions">
<%= f.submit "Update", :class => "submit_button round mini_button" %> <br>
</div>
<% end %>
</div>
This form submits to an update action in the users controller, and for the most part updates the user blurb and nothing else. However, one glitch is that this update action submits a nil password (as there is no password field in the form) - leading to a change in the user password. This is problematic for obvious reasons. Originally, I added a line :if => lamdba {new_record? || !password.nil? } to the password validation to allow the update from the edit_blurb partial to update without needing a password. But, as you can see this allows for a nil password to be saved.
validates :password, :presence => true,
:if => lambda{ new_record? || !password.nil? },
:confirmation => true,
:length => { :within => 6..40 }
How can I alter either the form or the validation (or both) so that a user can update a blurb without updating/inputting his password (and fixing the reset-to-nil password update problem)?
I think I've provided the relevant code, but will add more if it will help! Thanks everyone!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您想要做的是当密码值为 nil 时阻止用户模型保存密码。我不知道你的用户模型是如何设置的,但是如果你一直在关注MH的railstutorial,密码应该通过名为:
in User.rb的 方法保存。要解决此问题,请添加:
这应该可行。
what you'll want to do is prevent the user model from saving the password when the password value is nil. I don't know how your user model is set up, but if you've been following MH's railstutorial, the password should be saved by a method called:
in User.rb. To fix this, add:
This should work.