gpt4 book ai didi

ruby-on-rails - 嵌套形式和茧。未定义方法 `label' 为 nil :NilClass

转载 作者:太空宇宙 更新时间:2023-11-03 18:17:11 25 4
gpt4 key购买 nike

大家好。当我打开 /courses/new(或 /courses/some_id/edit)时,浏览器返回此错误:

Showing /app/views/dashboard/courses/_price.html.erb where line #1 raised:
undefined method `label' for nil:NilClass

这里是代码,_form.html.erb:

 <%= simple_form_for [:dashboard, @course], html: { multipart: true } do |f| %>

//////
<%= f.fields_for :prices do |p|%>
<%= render 'price', :f => 'prices' %>
<% end %>
<%= link_to_add_association 'Add', f, :prices %>

////////

_price.html.erb:

<%= p.label :price %>
<%= p.text_field :price %>
<%= p.label :desc %>
<%= p.text_field :description %>
<%= link_to_remove_association "remove", f %>

模型:

class Price < ActiveRecord::Base
belongs_to :course
end
class Course < ActiveRecord::Base
has_many :prices
accepts_nested_attributes_for :prices, :reject_if => :all_blank, :allow_destroy => true
end

如何解决这个错误?为什么会出现?

最佳答案

你正在使用simple_form_for,所以我猜是这一行

<%= f.fields_for :prices do |p|%>

应该是

<%= f.simple_fields_for :prices do |p|%>

看看 Git 了解更多信息。

关于ruby-on-rails - 嵌套形式和茧。未定义方法 `label' 为 nil :NilClass,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24290386/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com