2011-09-27 103 views
1

我一直在嘗試最近顯示的列表提交表單時成功修改的字段。唯一的問題是,我的表單(我使用簡單的表單)沒有顯示錯誤,當有一些表單無法提交。簡單的形式問題與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 

有沒有人的想法?

預先感謝您!

+0

你應該發佈你的視圖代碼。 –

+0

好的,這是張 – Cydonia7

+0

你從哪裏得到'assign_attributes'(它是rails 3.1特有的,'update_attributes'在以前的版本中更常用)?你在嚮導上進行大規模分配嗎? –

回答

3

它可能與您如何覆蓋AR有關。我記得一些插件遇到assign_attributes問題。同時你可以嘗試:

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

如果這樣做的話,它至少可以將問題縮小到質量分配。

+0

實際上,我解決了在fieldset_tag中封裝表單中文件的問題。我只是不明白爲什麼。 – Cydonia7

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

事實上,在我的其他形式,簡單的形式自動使用我自己的CSS樣式等。我在尋找的是這裏不屬於這種情況的原因。 – Cydonia7

+0

post ur查看代碼 –