I am using grape for my rails api development which is working pretty good for all the model except dynamic model form. We have following models..
1- product_type
2- product_fields
3- products
The product_type
is having having has_many
association with both product_fields
and products
model. While creating a new product_type
we can create various field attributes for that product
. Each product
has different attributes, but we store them in sing table "products". I want to generate the API dynamically whenever any product_type
is added.
I have tried as shown below but keep getting errors while posting the record. Appreciate any suggestion.
require 'grape'
module API
module V1
class Products < Grape::API
include API::V1::Defaults
@product_type=ProductType.all
@product_type.each do |producttype|
resource :"#{producttype.name}" do
desc "Create a new product for #{producttype.name}"
params do
requires :product_type_id , type: "Integer", desc: "product type Id"
producttype.productfields.each do |field|
if field.is_required?
requires :"#{field.field_name}" , type: "#{field.filed_type}", desc: "#{field.field_name}"
else
optional :"#{field.field_name}", type: "#{field.filed_type}", desc: "#{field.field_name}"
end
end
end
post do
Products.create!({
product_type_id:params[:product_type_id],
........
........
........
})
end
end
end
end
end
end
Error:
NoMethodError - undefined method `collect' for nil:NilClass:
grape-swagger (0.10.1) lib/grape-swagger.rb:70:in `block in combine_namespace_routes'
grape-swagger (0.10.1) lib/grape-swagger.rb:65:in `combine_namespace_routes'
grape-swagger (0.10.1) lib/grape-swagger.rb:39:in `add_swagger_documentation'
app/controllers/api/v1/base.rb:10:in `<class:Base>'
app/controllers/api/v1/base.rb:6:in `<module:V1>'
app/controllers/api/v1/base.rb:5:in `<module:API>'
app/controllers/api/v1/base.rb:4:in `<top (required)>'