0

好的,我整天都在查看结果(包括在 StackOverflow 上搜索)...

在 Rails 3.2.X 和 Ruby 1.9.3 中,我有一个远程表单。表单工作正常,有效时保存,无效时不保存。但是我无法找回实际的错误(我尝试了很多不同的方法)。

这是表格(haml)

= form_for @lead, :remote => true, :validate => false do |f|
    .span6
        %ul
            %li
                = f.label "Name*"
                = f.text_field :name
            %li
                = f.label "Company*"
                = f.text_field :company
...
            %li
                = f.label :state
                = select_tag :state, options_for_select(us_states)
            %li
                = f.label "Email*"
                = f.text_field :email
    .actions
        = f.submit '', :id => "contactsubmit"

这是控制器中的相关位置:

def create
    @lead = Lead.new(params[:lead])

    respond_to do |format|
        if @lead.valid?
         format.js
        else
         format.js
        end
    end
end

这是我一直失败的观点(create.js.erb)......

<% if @lead.errors.count == 0 %>
    $('#error_explanation').fadeOut(500);
    $('#learn-more .form').fadeOut(500, function() {
        $('#learn-more #form-response').fadeIn(500);
      });
<% else %>
      $('#error_explanation').fadeIn(500);
        <% @lead.errors.full_messages.each do |msg| %>
           #never shows any messages. If I do @lead.errors.each do |error| 
           #it shows the field name, but not the body...?
           $('#error_explanation').append('<%=content_tag(:p, msg)%>');
        <% end %>
<% end %>

我知道那里的评论无效,我添加它以显示我尝试过的内容。我已经尝试了一百万种不同的方式在这里显示错误消息。其他一切都在工作。我什至在其中添加了一个计数器(不使用 msg 时),它确实显示了四个字段错误。有什么建议吗?在另一个项目中使用类似的代码(在传统视图中)没有问题。

4

1 回答 1

0

确保您在escape_javascript遇到此类问题时使用。任何可能在内容中或由类似方法生成的引号都可能content_tag破坏您的 JS。

于 2012-08-08T23:44:39.947 回答