未定义的方法`坚持?'在link_to“毁灭”

时间:2015-09-28 16:43:52

标签: ruby-on-rails-4 view persistence

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

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

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

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

查看

<%= 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

1 个答案:

答案 0 :(得分:1)

我发现我试图删除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