gpt4 book ai didi

ruby-on-rails - 私有(private)方法 `update' 调用 #
转载 作者:数据小太阳 更新时间:2023-10-29 07:41:17 25 4
gpt4 key购买 nike

每当我尝试对我的 Customer 类进行更新时,我总是收到调用私有(private)方法“更新”的消息。

应用跟踪:
app/controllers/customers_controller.rb:46:在“更新”中

所以,在代码中它在这个函数中:

 43 def update
44 @customer = Customer.find(params[:id])
45
46 if @customer.update(customer_params)
47 redirect_to @customer
48 else
49 render 'edit'
50 end
51 end

因此,我假设此问题发生在我的客户模型中,即:

class Customer < ActiveRecord::Base
include ActiveModel::ForbiddenAttributesProtection
attr_accessible :name, :web_address, :notes
belongs_to :location
validates :name, presence: true,
length: { minimum: 5 }
end

然后在 View 中:

  <%= form_for :customer, url: customer_path(@customer), method: :put do |f| %>
<div class="well center col-sm-10">
<legend> Customer: </legend>
<span class="row">
<div class = "form-group col-sm-5">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>

<div class = "form-group col-sm-5">
<%= f.label :web_address %> <br />
<%= f.text_field :web_address %>
</div>
</span>
<span class="row">
<div class = "form-group col-sm-5">
<%= f.label :notes %> <br />
<%= f.text_field :notes %>
</div>

</span>

<%= f.submit %>
<% end %>

我从未定义过一个名为 update 的私有(private)函数,我也不确定从哪里开始调试它。

最佳答案

你应该使用 update_attributes , 适用于 Rails 4 以下的版本。

Persistence#update在 Rails4 之前是私有(private)的

关于ruby-on-rails - 私有(private)方法 `update' 调用 #<Customer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27199089/

25 4 0

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