2012-02-29 60 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