2012-02-29 84 views
1

我是一個完整的rails新手,我試圖爲聯繫表單創建驗證。我已經開始驗證代碼,但是如果我只加載我的聯繫頁面並單擊提交,它不會顯示任何驗證錯誤。試圖驗證聯繫表格

這裏是我認爲形式:

<%= form_for :message, :id => 'contactForm', :url => { :action => "create" } do |f|-%> 

     <%= label_tag 'name', 'Name:' %> 
     <%= f.text_field 'name', :class =>'required requiredField' %> 

     <%= label_tag 'email', 'Email:' %> 
     <%= f.email_field 'email', :class =>'required requiredField email' %> 

     <%= label_tag 'message', 'Message:' %> 
     <%= f.text_area 'message', :class => 'required requiredField', :rows => 20, :cols => 30 %> 

     <%= f.submit :class => 'button' do -%> 
      <a href="#" class="button" style="float:right;"><span>Submit</span></a> 
     <% end %> 
    <% end -%> 

控制器:

class ContactController < ApplicationController 

    layout "content" 

    def index 
     @activeLi = "contact" 
    end 

    def new 
     @message = Message.new 
    end 

    def create 
     @message = Message.new(params[:message]) 

     if @message.valid? 
      # need to actually send the email message here 
      flash[:notice] = "Message sent! We will get back to you shortly." 
      redirect_to root_url 
     else 
      render :action => 'new' 
     end 
    end 

end 

型號:

class Message 

    include ActiveModel::Validations 
    include ActiveModel::Conversion 
    extend ActiveModel::Naming 

    attr_accessor :name, :email, :message 

    validates_presence_of :name, :email, :message 
    validates_length_of :message, :maximum => 4000 

    def initialize(attributes={}) 
     attributes.each do |name, value| 
      send("#{name}=", value) 
     end 
    end 

    def persisted? 
     false 
    end 
end 

路線:

get "contact/index" 
post "contact/create" 
+0

或許應該的form_for(:消息......還什麼呢你的config/routes.rb文件看起來像 – 2012-02-29 02:06:17

+0

我把它改成。 :message並添加了我的routes.rb到問題 – Catfish 2012-02-29 02:18:12

回答

1

更改後線,你的routes.rb到

post 'contact/create' 
get 'contact/new' 
+0

我在我的視圖中更新了我的路線和我的form_for,現在我得到了這個錯誤'Missing template contact/new,application/new with {:handlers => [:erb,:builder,:coffee],:formats => [:html],:locale => [:en,:en]}。爲什麼它不能識別我的ContactController佈局內容中的這一行。 – Catfish 2012-02-29 04:54:40

+0

加上'get'contact/new''也 – 2012-02-29 05:25:07

+0

沒有做任何不同的事情。還是一樣的錯誤。 – Catfish 2012-02-29 05:31:02

2

您正在將表單發回到:index操作,但沒有在:index操作中的任何位置嘗試創建並保存ContactModel的實例。直到你這樣做,它永遠不會驗證。你想添加這樣的東西到你的:索引行動:

如果request.post? 釐米= ContactModel.new(... PARAMS去這裏...) 如果cm.save #一切都好 其他 #吐出錯誤 結束 結束

我建議不要命名模型「 ContactModel「(擺脫'模型')。 ContactRequest,ContactSubmission等等。

此外,檢查出http://railscasts.com/episodes/326-activeattr。非常適用於你在做什麼。

最後,將您的form_tag切換到form_for並使用您的模型的新實例。一直保持乾淨整潔。

+0

我看了一下這個截屏以及219號碼。我也用我的最新代碼更新了我的帖子,但它仍然沒有顯示任何驗證錯誤。如果你要求提供request.post?....',我沒有在screencast中看到這個,還有,我是否需要在我的視圖中放置一些東西,以便在那裏顯示驗證錯誤? – Catfish 2012-02-29 01:29:38

1

我是Rails自己的新手 - 一直都只學了一個月。如果我理解你是對的,你希望在提交表單時顯示錯誤。由於模型中的validates_presence_of,如果提交時沒有正確的值,我希望你的模型不會被保存到數據庫中。您的form_for標記下方可能需要以下代碼。然後會顯示任何驗證錯誤。

<% if @message.errors.any? %> 
<h4>Errors</h4> 
<ul> 
    <% @message.errors.full_messages.each do |message| %> 
     <li><%= message%></li> 
    <% end %> 
</ul> 
<% end %> 
+0

它沒有保存任何數據庫,因爲我的模型沒有擴展ActiveRecord。我試過你的代碼,但現在它會拋出一個錯誤:'當你沒有想到它時,你有一個無對象! 您可能已經預期了ActiveRecord :: Base的實例。 評估nil.errors時發生錯誤# – Catfish 2012-02-29 02:00:38

+0

@Catfish請嘗試我更新的代碼..看起來像在視圖中的activerecord是零。我有一個大寫的M而不是小寫字母m(你的活躍記錄) – Prashanth 2012-02-29 02:06:44

+0

我試過你的更新代碼。同樣的錯誤。 – Catfish 2012-02-29 02:19:14