开发者

error message not rendering in containing div

开发者 https://www.devze.com 2023-03-18 18:54 出处:网络
I have a form that looks like this - <%= form_for @photo,:html=>{:multipart=>true} do |f| -%>

I have a form that looks like this -

<%= form_for @photo,:html=>{:multipart=>true} do |f| -%>
<div> 
       <%= f.file_field :photo %>
       <%= f.submit "Upload", :disable_with => 'Uploading...' %>
</div>
    <%= @photo.errors[:photo].each do |attr,msg| %>
         <span class="form-error"><%= msg %></span>
    <% end %>   
<开发者_C百科;% end -%>

My model has the following line

  validates_presence_of :photo, :message => "Choose a file to upload"

For some reason the error message is rendering outside of containing span. The rendered HTML looks like this -

<form ..some attributes..><some hidden divs and inputs />
  <div class="field_with_errors"><input type="file" name="photo[photo]" id="photo_photo"></div>
  <input type="submit" value="Upload" name="commit" id="photo_submit" data-disable-with="Uploading...">
  <span class="form-error"></span>
  Choose a file to upload
</form>

I'm very new to rails, but this seems odd to me.

Why is my message outside of it's container?

Thanks.


Shouldn't this NOT have an equal sign (you like my double negative there)?

<%= @photo.errors[:photo].each do |attr,msg| %>
 / \
  |
  |

In other words, it should be:

<% @photo.errors[:photo].each do |attr,msg| %>

I've made this typo about a hundred times, and have finally got out of the habit of doing it. I'll bet that is part of the issue.

Your form_for needs and equal sign, because it outputs HTML into your view. The loop itself does not, because it is just a loop.

0

精彩评论

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