Rails 3 的简单表单问题
我最近一直在尝试显示在提交表单时成功修改的字段列表。唯一的问题是,当有一些错误并且表单无法提交时,我的表单(我使用简单表单)不会显示错误。
这是我的简化代码:
def update
@wizard.assign_attributes(params[:wizard])
# Get changed attributes
if @wizard.save
# Set the success flash with fields modified
redirect_to @wizard
else
@title = "Edition du profil"
render 'edit'
end
end
视图:
<%= simple_form_for @wizard do |f| %>
<%= f.input :email %>
<%= f.input :story %>
<%= f.submit "Modifier", :class => "btn success small" %>
<% end %>
模型:
class Wizard < ActiveRecord::Base
has_secure_password
attr_accessible :email, :story, :password, :password_confirmation, :password_digest
serialize :ranks, Array
validates_presence_of :email, :first_name, :last_name, :gender, :story
validates_presence_of :password, :password_confirmation, :unless => Proc.new { |w| w.password_digest.present? }
# Other validations here
has_one :subject, :foreign_key => "teacher_id"
ROLES = %w[teacher]
scope :with_role, lambda { |role| {:conditions => "roles_bitmask & #{2**ROLES.index(role.to_s)} > 0"} }
# Other functions here
end
有人有想法吗?
先感谢您 !
I've been trying recently to show a list of the fields modified with success on submitting a form. The only problem is that my form (I use simple form) doesn't show the errors when there are some and the form can't be submitted.
Here's my code simplified :
def update
@wizard.assign_attributes(params[:wizard])
# Get changed attributes
if @wizard.save
# Set the success flash with fields modified
redirect_to @wizard
else
@title = "Edition du profil"
render 'edit'
end
end
The view :
<%= simple_form_for @wizard do |f| %>
<%= f.input :email %>
<%= f.input :story %>
<%= f.submit "Modifier", :class => "btn success small" %>
<% end %>
The model :
class Wizard < ActiveRecord::Base
has_secure_password
attr_accessible :email, :story, :password, :password_confirmation, :password_digest
serialize :ranks, Array
validates_presence_of :email, :first_name, :last_name, :gender, :story
validates_presence_of :password, :password_confirmation, :unless => Proc.new { |w| w.password_digest.present? }
# Other validations here
has_one :subject, :foreign_key => "teacher_id"
ROLES = %w[teacher]
scope :with_role, lambda { |role| {:conditions => "roles_bitmask & #{2**ROLES.index(role.to_s)} > 0"} }
# Other functions here
end
Has anyone an idea ?
Thank you in advance !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可能和你覆盖AR的方式有关。我记得一些插件在分配属性时遇到了麻烦。同时您可以尝试:
如果有效,它至少会将问题范围缩小到批量分配。
It has probably something to do with how you overwrote AR. I remember some plugin getting in trouble with assign_attributes. Meanwhile you can try :
If that works it will at least narrow down the problem to mass assignment.
您可能在编辑/新视图中缺少这部分。其中
@wizard
是您的 model_name。将这段代码写在form标签中。
you perhaps missing this part in edit/new view.Where
@wizard
is your model_name.Write this piece of code in the form tag.