我是一個完整的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"
或許應該的form_for(:消息......還什麼呢你的config/routes.rb文件看起來像 – 2012-02-29 02:06:17
我把它改成。 :message並添加了我的routes.rb到問題 – Catfish 2012-02-29 02:18:12