【问题标题】:Unable to user link_to_add_fields method from railscast Nested Model Form #196/#197无法使用 railscast 嵌套模型表单中的 link_to_add_fields 方法 #196/#197
【发布时间】:2012-02-14 12:15:13
【问题描述】:

首先我看了Railscast #196Railscast #197

我还阅读了this 和许多其他类似的问题。

我收到以下错误:

Missing partial profils/compagny_fields, application/compagny_fields with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}. Searched in: * "/home/me/www/Myapp/app/views"

这似乎不是部分问题,因为我可以从我的部分看到puts

所以在我的模型上我有:

CompagnyModel:belongs_to :profil`

ProfilModel:has_many :compagnies,accepts_nested_attributes_for :compagnies

在我看来,我有:

<%= p.fields_for :compagnies do |n| %>
   <%= render 'compagnies_fields', :c => n %>
<% end %>
<%= link_to_add_fields "Add Compagny", p, :compagnies %> 
<% end %>

最后是我的部分:

<p class="fields">
  <%= c.label :name, "Compagny"%><br />
  <%= c.text_field :name %><br />
  <%= link_to_remove_fields "remove", c %><br />
  <% puts "-----!-!-!-!-!-!-!-!-DEBUG-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!------" %>   
</p>

【问题讨论】:

    标签: ruby-on-rails ruby activerecord web


    【解决方案1】:

    是单引号吗

    CompagnyModel: belongs_to :profil`
    

    在实际的模型文件中?

    被调用的部分表单是保存在“profiles/_compagny_fields.html.erb”还是“compagnies/_compagny_fields.html.erb”中?

    <%= p.fields_for :compagnies do |n| %>
      <%= render 'compagny_fields', :c => n %>
    <% end %>
    

    尝试在控制台中构建 Profil.Compagnies 模型以检查它是否有效

    new_profil = Profil.new
    new_profil.compagnies.build
    

    我认为在 Profil_Controller 中有一个变量 @profil = Profil.new?

    @profil = Profil.new
    @profil.compagnies.build
    

    views/profil/new.html.erb(或views/profil/_form.html.erb,如果你以这种方式处理表单)是否有表单

    <%= form_for(@profil) do |p| %>
    

    【讨论】:

      【解决方案2】:

      我有同样的错误。您将部分命名为“compagny_fields”,但您想渲染“compagnies_fields”。这是我的问题,也许它也是你的。

      【讨论】:

        【解决方案3】:

        我通过改变这个来解决这个问题

        render(association.to_s.singularize + "_fields", :f => builder)
        

        到这里

        render(association.to_s , :f => builder)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-08-13
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多