开发者

how do I place custom error messages at the top of a form?

开发者 https://www.devze.com 2023-02-15 03:52 出处:网络
Such a novice question, but I can\'t seem to resolve this. if I have a simple Rails 2.3.5 form : - form_for CardSignup.new do |f|

Such a novice question, but I can't seem to resolve this.

if I have a simple Rails 2.3.5 form :

- form_for CardSignup.new do |f|
   = f.error_messages
  .grid_1.alpha.align_right
    = f.label :zip_code, 'zip'
  .grid_1.omega.开发者_运维知识库alpha
    = f.text_field :zip_code, :style => "width: 75px;"
    %div{:class => 'error_message'}
  .clear

How do I make the error_message appear at the top of the form, rather than in the particular place of the form?

I was using this : = f.error_messages , but it doesn't show any content when put at the top of the form.


hah I was looking for this too. Anyways this is how I figured it out. So all we need to do is update flash[:alert] or whatever and have that flash[:alert] at the top of your layout file. Now all u do if

<%= simple_form_for(@boing) do |f| %>
  <% if @boing.errors.any? %>
    <% flash[:alert] = "stuff" %>
  <% end %>
....

now you don't want <%= flash[:alert] = "stuff" %> otherwise it will display at both places-inline and at top as <%= %> is for displaying in browser. Hope that helps


Why are you using CardSignup.new in the view rather than defining @card_signup = CardSignup.new in the controller? If you are doing like that you can call

<%= @card_signup.error_messages %>

or

<%= error_messages_for :card_signup %>

anywhere in the view file and it will show the error messages for @card_signup object and you can call your form like this:

<% form_for @card_signup do |f| %>
0

精彩评论

暂无评论...
验证码 换一张
取 消