我正在开发 Rails 4 应用程序,最近遇到了一个奇怪的问题。我在这里寻求你的帮助。请指教。
已创建一个小要点片段来理解问题 undefined method committed?
总结一下:
# app/models
class User < ActiveRecord::Base
has_many :responses, dependent: :destroy
end
class Response < ActiveRecord::Base
has_one :report
has_many :points
belongs_to :user
end
class Report < ActiveRecord::Base
belongs_to :response
end
class Point < ActiveRecord::Base
belongs_to :response
end
# config/routes.rb
resources :users do
resources :responses do
resources :action_plans
end
end
# app/controllers/action_plans_controller.rb
class ActionPlansController < ApplicationController
before_filter :response
def new
@report = @response.build_report
5.times do
@response.points.build
end
end
private
def response
@response = current_user.responses.find(params[:id])
end
end
无论何时,我都在尝试:
http://localhost:3000/users/:user_id/responses/:id/action_plans/new
我收到错误消息:未定义的方法“已提交?”对于响应对象。我在这里做错了什么?
最佳答案
通过定义一个名为 response
的方法在您的 Controller 中,您正在覆盖 Rails 使用的内部 getter。要解决此问题,只需为之前的操作使用不同的名称。命名 Action 的常用方法是使用 set_<entity name>
所以set_response
是的。
关于ruby-on-rails - 模型对象的 Rails4 : undefined method `committed?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27359655/