未定义的方法“持续存在?”在 link_to "Destroy"

undefined method `persisted?' in link_to "Destroy"

<%= link_to "Destroy", builder, :confirm => 'Are you sure?', :method => :delete %> 显示错误

没有方法 - 未定义的方法持续存在?

Builder(Experience) 是 User 中的一个嵌套属性,允许破坏 experience

我添加了持久化?用户和体验控制器中的方法,但仍然无法正常工作。

查看

<%= form_for(@user) do |f| %>
<%= f.fields_for :experiences do |builder| %>
    <%= builder.text_field :title, class: 'form-control form-group' %>
    <%= link_to "Destroy", builder, :confirm => 'Are you sure?', :method => :delete %>
<% end %>
<% end %>

体验控制器

class ExperiencesController < ApplicationController
  def index
    @experience = Experience.all
  end
def show
    @experience = Experience.find(params[:id])
end
def destroy
    @experience = Experience.find(params[:id])
    @experience.destroy
    flash[:notice] = "Successfully destroyed experience."
    redirect_to profile_path
end
def persisted?
  true
end
end

经验模型

class Experience < ActiveRecord::Base
    belongs_to :user
    validates_presence_of :user
end

用户模型

class User < ActiveRecord::Base
    has_many :experiences
    accepts_nested_attributes_for :experiences, reject_if: proc { |attributes| attributes['title'].blank? }, allow_destroy: true
 end

路线

experience GET    /experiences/:id(.:format)         experiences#show
                     PATCH  /experiences/:id(.:format)            experiences#update
                     PUT    /experiences/:id(.:format)         experiences#update
                     DELETE /experiences/:id(.:format)         experiences#destroy

我发现我试图删除一个 field_for 而不是那个 field_for 的对象,我只是在 link_to 中添加 builder.object,就像这样

<%= link_to "Destroy", builder.object, :confirm => 'Are you sure?', :method => :delete %>

这帮助我找到了解决方案Get object value with nested_form