Created
May 16, 2011 13:55
-
-
Save Porta/974480 to your computer and use it in GitHub Desktop.
ProductsController
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ProductsController < ApplicationController | |
#OJO ACA, esto limita los responses a SOLO json. podes agregar mas | |
respond_to :json | |
def index | |
resultado = Array.new | |
Product.find_each do |p| | |
resultado.push(p_para_json(p)) | |
end | |
render :json => resultado | |
end | |
def show | |
begin | |
@post = Product.find(params[:id]) | |
rescue | |
render :json => "ERROR" | |
return false | |
end | |
respond_with(@post) | |
#render :json => p_para_json(p) | |
end | |
private | |
def pp_para_json(pp) | |
resultado = Array.new | |
pp.find_each do |pp| | |
line = Hash.new | |
line[:name] = pp.property.name | |
line[:value] = pp.value | |
resultado.push(line) | |
end | |
return resultado | |
end | |
end | |
################################# | |
class Product < ActiveRecord::Base | |
#... | |
def as_json(options = {}) | |
{ | |
:id => self.id, | |
:product_type => self.product_type.name | |
:property_values => #esto lo podes hacer un con inject sobre el array. | |
} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment