【问题标题】:Getting undefined method `klass' for nil:nilclass - Railscast #197为 nil:nilclass 获取未定义的方法 `klass' - Railscast #197
【发布时间】:2011-11-19 04:36:10
【问题描述】:

大家好,我在嵌套模型和表单上关注了 Ryan Bates 的 railscast,但我得到了 nil:nilclass 的未定义方法“klass”。我很确定这是由于 link_to_add_fields 造成的,因为之前一切正常。下面是我的错误和其他相关代码,我使用的是 Rails 3.1。我做了很多谷歌搜索,但没有找到任何解决我的问题的方法,所以如果你们能帮助我,我将非常感激。感谢您的帮助。

_form.html.erb

<%= form_for(@organization) do |f| %>
  <% if @organization.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@organization.errors.count, "error") %> prohibited this organization from being saved:</h2>

      <ul>
      <% @organization.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
    <div id="restaurant_field" class="field">
        <%= f.fields_for :restaurants do |builder| %>
            <%= render 'organizations/partials/restaurant_fields', :f => builder %>
        <% end %>
    </div>

  <div class="actions"><%= f.submit %></div>
<% end %>

_restaurant_fields.html.erb

<p class="fields">
<%= f.label :name, "Restaurant Name" %><br />
<%= f.text_field :name %>
<%= link_to_remove_fields "Remove", f %>

application_helper.rb

    module ApplicationHelper
  def link_to_remove_fields(name, f)
      f.hidden_field(:_destroy) + link_to_function(name, "remove_fields(this)")
    end

    def link_to_add_fields(name, f, association)
      new_object = f.object.class.reflect_on_association(association).klass.new
      fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
        render(association.to_s.singularize + "_fields", :f => builder)
      end
      link_to_function(name, h("add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")"))
    end
end

application.js

function remove_fields(link) {  
    $(link).prev("input[type=hidden]").val("1");  
    $(link).closest(".fields").hide();  
}  

function add_fields(link, association, content) {  
    var new_id = new Date().getTime();  
    var regexp = new RegExp("new_" + association, "g");  
    $(link).parent().before(content.replace(regexp, new_id));  
}

【问题讨论】:

    标签: ruby-on-rails-3.1 nested-forms railscasts


    【解决方案1】:

    我发现如果关联模型由 has_one 描述,则 link_to_add_fields 助手将不起作用。 has_one 表示关联没有获取到 klass 对象。

    您可以通过更改与 has_many :object + s(您的对象名称带有 s)的关系并将您的对象以复数形式传递给 link_to_add_fields 来确定这是您的问题。

    【讨论】:

      【解决方案2】:

      你和这个人here做了同样的事情

      编辑:(我的错误)

      我假设您在此行下方有一个 link_to_add_fields

      <%= link_to_remove_fields "Remove", f %>
      

      似乎 _restaurant_fields.html.erb 部分不完整。 (没有结束标签)

      </p>
      

      删除 f.fields_for 之外的 link_to_add_fields

      这应该可以解决类错误。

      【讨论】:

      • 谢谢。我会试一试,然后回复你。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-05
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      • 2011-07-27
      • 2013-06-30
      • 1970-01-01
      相关资源
      最近更新 更多