代码之家  ›  专栏  ›  技术社区  ›  Vincent

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

  •  2
  • Vincent  · 技术社区  · 14 年前

    在我的rails3应用程序中,我有一个典型的“Email this to a friend”表单,其中包含您/朋友的Email和name字段。必须以类似于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>
    

    做这件事的“铁路方式”是什么?

    1 回复  |  直到 14 年前
        1
  •  1
  •   Vincent    14 年前