我有偉大的工程如下:Rails中,模型關係問題
@request_thread = current_user.request_threads.new(params[:request_thread])
而且我有這個偉大的工程:
@requestable = find_requestable
@request_thread = @requestable.request_threads.new(params[:request_thread])
但是,如果我嘗試用2號線:
@request_thread = @requestable.current_user.request_threads.new(params[:request_thread])
隨着current_user我得到以下錯誤:
NoMethodError (undefined method `current_user' for #<Photo:0x10f95c828>):
app/controllers/request_threads_controller.rb:52:in `create'
app/middleware/flash_session_cookie_middleware.rb:14:in `call'
我在這裏弄到了什麼?
感謝
更新 - 與find_requestable
# http://asciicasts.com/episodes/154-polymorphic-association
def find_requestable
params.each do |name, value|
if name =~ /(.+)_id$/
return $1.classify.constantize.find(value)
end
end
nil
end
也許這樣的事情... @ requestable.find_by_user(CURRENT_USER).request_threads.new(PARAMS [:request_thread]) – mpapis 2010-11-12 01:18:05