在rails 5中设置多态关系时出现NoMethod错误。
我已按照此处的说明进行操作:https://guides.rubyonrails.org/association_basics.html#polymorphic-associations
我正在运行5.2
这是我的模特:
class Image < ApplicationRecord
belongs_to :imageable, polymorphic: true
validates :url, :presence => true
end
class Trip < ApplicationRecord
validates :name, :presence => true
# validates :start_date, :presence => true
# validates :end_date, :presence => true
belongs_to :user
has_many :todo, :dependent => :destroy
has_many :destination, :dependent => :destroy
has_many :events, through: :destination
has_many :image, as: :imageable
def formatted_arrival_time
self.arrival_time.strftime("%H:%M")
end
def formatted_departure_time
self.departure_time.strftime("%H:%M")
end
end
class Destination < ApplicationRecord
validates :title, :presence => true
belongs_to :trip
has_many :event, :dependent => :destroy
has_many :image, as: :imageable
end
然后在我的控制器中:
# GET /images
def index_trip
if params[:page] && params[:per_page]
trip = Trip.find(params[:trip_id])
@images = Trip.image.all
paginate json: @images, meta: {
total: @images.count,
per_page: params[:per_page].to_i,
page: params[:page].to_i,
pages: (@images.count / params[:per_page].to_f).ceil
}
else
trip = Trip.find(params[:trip_id])
@images = Trip.image.all
render json: @images
end
end
任何关于我在这里做错事情的想法将不胜感激。
答案 0 :(得分:0)
结果发现Trip上的小写字母应该是“ trip”