'shared / error_messages'部分不应该呈现

时间:2012-08-07 14:50:00

标签: ruby-on-rails

我无法弄清楚为什么我的'shared / error_messages'部分未被呈现(即当通过此表单提交无效'处理'时):

_treating_form.html.erb:

<%= form_for(@treating) do |f| %>  
    <%= render 'shared/error_messages', object: f.object %>
    <div><%= f.hidden_field :requestee_id %></div>  
    <div>
        <%= f.text_field :proposed_location, placeholder: "Propose a location here..." %>
    </div>    
    <div>
        <%= f.text_field :proposed_date, placeholder: "Propose a date here..." %>
    </div>
    <div class="field">
        <%= f.text_area :intro, placeholder: "Write your introduction here..." %>
    </div>
    <%= f.submit "Send", class: "btn btn-large btn-primary" %>
<% end %>

此_treating_form.html.erb部分在users / show.html.erb视图中调用:

<% provide(:title, @user.name) %>
<div class="row">
    <aside class="span4">
        <section>
            <h1>
                <%= @user.name %>
            </h1>
        </section>
        <% if signed_in? %>
            <section>
                    <%= render 'shared/treating_form' unless current_user?(@user) %>
            </section>
        <% end %>
    </aside>
</div>

这是我的error_messages partial:

<% if object.errors.any? %>
    <div id="error_explanation">
        <div class="alert alert-error">
            The form contains <%= pluralize(object.errors.count, "error") %>.
        </div>
        <ul>
            <% object.errors.full_messages.each do |msg| %>
                <li>* <%= msg %></li>
            <% end %>
        </ul>
    </div>
<% end %>

在提交到用户“修改设置”页面时触发验证错误时,会正确呈现error_messages部分:

<% provide(:title, "Edit user") %>
<h1>Update your profile</h1>
<div class="row">
    <div class="span6 offset3">
    <%= form_for(@user) do |f| %>
        <%= render 'shared/error_messages', object: f.object %>
        <%= f.label :name %>
        <%= f.text_field :name %>
        <%= f.label :email %>
        <%= f.text_field :email %>
        <%= f.label :password %>
        <%= f.password_field :password %>
        <%= f.label :password_confirmation, "Confirm Password" %>
        <%= f.password_field :password_confirmation %>
        <%= f.submit "Save changes", class: "btn btn-large btn-primary" %>
        <% end %>       
    </div>
</div>

在提交无效“处理”时,为什么没有显示错误的任何想法?谢谢!

1 个答案:

答案 0 :(得分:1)

每当您向部分添加任何选项时,您需要明确指定:partial =&gt;

= render partial: 'shared/error_messages', object: f.object