【问题标题】:Use "error_messages" in Rails 3.2? (raises "undefined method" error)在 Rails 3.2 中使用“error_messages”? (引发“未定义方法”错误)
【发布时间】:2012-04-17 15:08:42
【问题描述】:

我在 Rails 3.2 功能测试中遇到以下错误:

ActionView::Template::Error: undefined method `error_messages' for # <:helpers::formbuilder:0x007ff8ad00d3b0>

产生错误的视图代码:

<%= form_for [@camp, @program] do |f| %>
  <%= f.error_messages %> # problematic code
  <%= f.label :name %>
<% end %>

这是我的控制器中调用上述视图代码的代码:

render :action => "edit", :status => :bad_request

这是我正在运行的测试:

test "update a program with a bad request" do
  put :update, 
    :id => programs(:traditional).to_param, 
    :program => {
      :min_age => "a" 
    },
    :camp_id => camps(123).uri

  assert_response :bad_request
end

有没有人知道为什么在 Rails 3.2 应用程序中会出现此错误?

谢谢!

【问题讨论】:

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-3.2


【解决方案1】:

尝试使用以下代码迭代所有错误(如果有)。

<%= form_for [@camp, @program] do |f| %>
   <% @program.errors.full_messages.each do |msg| %>
      <p><%= msg %></p>
   <% end %>
<%= f.label :name %>
<% end %>

【讨论】:

  • 应该通过部分渲染来完成。 render partial: 'error', collection: @program.errors.full_messages
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多