我正在尝试找到一种优雅(标准)的方法来将多态模型的父级传递给视图.例如:
class Picture < ActiveRecord::Base
belongs_to :imageable, :polymorphic => true
end
class Employee < ActiveRecord::Base
has_many :pictures, :as => :imageable
end
class Product < ActiveRecord::Base
has_many :pictures, :as => :imageable
end
Run Code Online (Sandbox Code Playgroud)
以下方式(find_imageable)有效,但似乎"hackish".
class PictureController < ApplicationController
#/employees/:id/picture/new
#/products/:id/picture/new
def new
@picture = imageable.pictures.new
respond_with [imageable, @picture]
end
private
def imageable
@imageable ||= find_imageable
end
def find_imageable
params.each do |name, value|
if name =~ /(.+)_id$/
return $1.classify.constantize.find(value)
end
end
nil
end
end
Run Code Online (Sandbox Code Playgroud)
有没有更好的办法?
编辑
我正在做一个new动作.路径采用的形式parent_model/:id/picture/new …
我正在制作一个游戏,其中某个物体(建模为box2d体)必须遵循固定的路径.有没有办法可以指定路径坐标并使对象在每个dt上前进?
谢谢
我必须发送一个嵌套的json请求,其中包含内部层次结构中的图像.例如:
{"product" : {
"catalogue_id" : "x",
"name" : "my product",
"image" : #<image>
}
}
Run Code Online (Sandbox Code Playgroud)
问题是,如果我尝试使用multipartFormRequestWithMethod:path:parameters:constructingBodyWithBlock:(和appendPartWithFileData:name:fileName:mimeType:),将catalogue_id和name命名为params,则在"product"之后附加图像字段,如下所示:
{"product" : {
"catalogue_id" : "x",
"name" : "my product"
} ,
"image" : #<image>
}
Run Code Online (Sandbox Code Playgroud)
有没有办法指定图像字段嵌套在一定深度?
谢谢堆