1

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

这是我的简化代码:

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

有人有想法吗?

先感谢您 !

4

2 回答 2

3

这可能与您如何覆盖 AR 有关。我记得有些插件遇到了assign_attributes 的问题。同时您可以尝试:

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

如果这行得通,它至少会将问题缩小到大规模分配。

于 2011-10-05T12:04:30.270 回答
0

您可能在编辑/新视图中错过了这部分。@wizard您的模型名称在哪里。将这段代码写在表单标签中。

<% 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 %>
于 2011-09-27T13:24:09.303 回答