2011-06-11 16 views
2

我使用formtastic驗證形式:軌道/ Formtastic - 確認不存在的

<%= semantic_form_for @user do |f| %> 
    <ul class="formItem">   
    <%= f.input :username, :required => true, :label => "Your Username", :input_html => { :class => 'double' } %><br clear="all" /> 
    <p class="fieldExplanation">Your username is made public to other people on the site.</p>      
    </ul> 
    <ul class="actions">  
    <%= f.commit_button("Save my profile and make a match >", :class => "submitForm") %>  
    </ul> 
<% end %> 

它的視覺工作 - 形式一點點呈現星號來表示,它理解:要求=>真 - 但是當頁面發佈時,它不會觸發具有錯誤的驗證;相反,它只是轉到下一頁,就好像它是成功的。

我覺得我錯過了一個簡單的設置某處開啓驗證,或者我在我的控制器中做錯了什麼?

def update 
@user = User.find(params[:id])  
respond_to do |format| 
    if @user.update_attributes(params[:user])   
     format.html { redirect_to(match_user_path(@user), :notice => 'User was successfully updated.') }    
    else 
    format.html { render :action => "edit" }   
    end 
end 
end 

在此先感謝。

+0

我開始認爲別的東西壞了 - 當我包含'<%= f.input:mySelectBox,as =>:select,:collection => [「Thing one」,「Thing two」]%> '我得到了**未定義的局部變量或方法'as'for#<#:0x837a368> ** – Jon 2011-06-11 15:59:51

回答

1

您在評論中顯示的代碼中省略了冒號。它應該是:as,這將解釋一些破損。

就驗證而言,這是在您的模型中執行的。你需要的東西,如:

validates :username, :presence => true 

使代碼在服務器端了解,在未經username,該模型是無效的。一旦你完成了所有的服務器端驗證,如果你想迅速將這些驗證轉換爲表單,請查看validation_reflections gem。你可以閱讀關於它on ASCIIcasts here

0

Formtastic是只有呈現一個表單,所以驗證需要在模型中完成服務器端。 Formtastic將使用這些驗證來推斷輸入是否應該標記爲需要或不需要。 DSL中的:required選項實際上只是爲了在極少數情況下覆蓋此選項,在這種情況下,您無法在模型驗證中執行所需的操作。