Rails 3的简单表单问题

时间:2011-09-27 13:08:38

标签: ruby-on-rails-3 ruby-on-rails-3.1 simple-form

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

这是我的代码简化:

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

有人有想法吗?

提前谢谢!

2 个答案:

答案 0 :(得分:3)

这可能与你如何覆盖AR有关。我记得有些插件在使用assign_attributes时遇到了麻烦。同时你可以试试:

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

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

答案 1 :(得分:0)

你可能在编辑/新视图中遗漏了这个部分。@wizard是你的model_name。 在表格标签中写下这段代码。

<% 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 %>