小编Nav*_*Nav的帖子

Rails 3:在控制器中找到多态模型的父级?

我正在尝试找到一种优雅(标准)的方法来将多态模型的父级传递给视图.例如:

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".

#PictureController(已更新,包括完整列表)

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 …

polymorphic-associations ruby-on-rails-3

12
推荐指数
1
解决办法
6747
查看次数

使Box2d对象遵循预定路径

我正在制作一个游戏,其中某个物体(建模为box2d体)必须遵循固定的路径.有没有办法可以指定路径坐标并使对象在每个dt上前进?

谢谢

box2d cocos2d-iphone box2d-iphone

4
推荐指数
1
解决办法
2438
查看次数

AFNetworking在嵌套json中发布图像

我必须发送一个嵌套的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)

有没有办法指定图像字段嵌套在一定深度?

谢谢堆

ios afnetworking

3
推荐指数
1
解决办法
2709
查看次数