【发布时间】:2019-02-04 03:28:55
【问题描述】:
我有一个 Location 模型,它通过 :locatable 实现多态
一个Event 有一个Location:
class Event < ApplicationRecord
has_one :location, as: :locatable
end
在events_controller#create 中,如何使用通过参数传入的位置创建一个新事件?
def create
property = Event.create! params.require(:event).permit(:name, :time, :location, :organizer_id, attachments: [])
# ... other stuff, and finally render JSON response
end
但是,Event.location 是 nil。如何为活动创建地点?
谢谢
【问题讨论】:
-
您想创建一个新位置还是只是将现有位置与新事件相关联?
-
在您执行该请求时显示您的服务器日志。
:location允许的哈希引用单个值,也检查这个问题 stackoverflow.com/questions/3969025/…
标签: ruby-on-rails activerecord ruby-on-rails-5