Rails 3 的简单表单问题

发布于 2024-12-06 15:02:11 字数 1281 浏览 1 评论 0原文

我最近一直在尝试显示在提交表单时成功修改的字段列表。唯一的问题是,当有一些错误并且表单无法提交时,我的表单(我使用简单表单)不会显示错误。

这是我的简化代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

π浅易 2024-12-13 15:02:11

可能和你覆盖AR的方式有关。我记得一些插件在分配属性时遇到了麻烦。同时您可以尝试:

@wizard.assign_attributes(params[:wizard], :without_protection => true)

如果有效,它至少会将问题范围缩小到批量分配。

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 :

@wizard.assign_attributes(params[:wizard], :without_protection => true)

If that works it will at least narrow down the problem to mass assignment.

她如夕阳 2024-12-13 15:02:11

您可能在编辑/新视图中缺少这部分。其中 @wizard 是您的 model_name。
将这段代码写在form标签中。

<% if @wizard.errors.any? %>
        <div id="error_explanation">
          <h2><%= pluralize(@wizard.errors.count, "error") %> prohibited this task from being saved:</h2>

          <ul>
            <% @wizard.errors.full_messages.each do |msg| %>
                <li><%= msg %></li>
            <% end %>
          </ul>
        </div>
    <% end %>

you perhaps missing this part in edit/new view.Where @wizard is your model_name.
Write this piece of code in the form tag.

<% if @wizard.errors.any? %>
        <div id="error_explanation">
          <h2><%= pluralize(@wizard.errors.count, "error") %> prohibited this task from being saved:</h2>

          <ul>
            <% @wizard.errors.full_messages.each do |msg| %>
                <li><%= msg %></li>
            <% end %>
          </ul>
        </div>
    <% end %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文