简体   繁体   English

Rails:验证和显示不在模型中的字段的错误消息的最佳方法

[英]Rails: best way to validate and display error messages for fields not in model

In my Rails 3 application I have a typical 'Email this to a friend' form that has yours/friend's email and name fields. 在我的Rails 3应用程序中,我有一个典型的“将此邮件发送给朋友”表格,其中包含您/朋友的电子邮件和姓名字段。 Each of them must be validated and error messages must be displayed, in a fashion similar to ActiveRecord forms: 它们中的每一个都必须经过验证,并且必须以类似于ActiveRecord形式的方式显示错误消息:

  <%= form_tag %>
    <div class="fields">
      <%= label_tag :friends_name %>
      <%= text_field_tag :friends_name, params[:friends_name] %>
      <%= label_tag :friends_email %>
      <%= text_field_tag :friends_email, params[:friends_email] %>
    </div>
    <div class="fields">
      <%= label_tag :your_name %>
      <%= text_field_tag :your_name, params[:your_name] %>
      <%= label_tag :your_email %>
      <%= text_field_tag :your_email, params[:your_email] %>
    </div>
    <div class="fields">
      <%= label_tag :message %>
      <%= text_area_tag :message, params[:message] %>
    </div>
    <%= submit_tag 'Send'%>
  </form>

What would be the 'Rails way' of doing this? 这样做的“路轨方式”是什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM