Pass rails method(association) to React Js
Asked Answered
T

0

0

In My React js application I need to pass data of my Product Model's association model(say "Taxon"),i am doing it successfully using Active record serializer as

ActiveModelSerializers::SerializableResource.new(object, options).serializable_hash

where i pass options as all association relation.
i can get whole array of taxon objects as json,But I want something mentioned As following

i using rails as backed i am calling method like

Prodct.first.category.name 

which gives => "mugs"

the method it calls is

def category
  taxons.joins(:taxonomy).find_by(spree_taxonomies: { name: Spree.t(:taxonomy_categories_name) })
end

Which give specific taxon object,In rails its working fine

But i need to pass it to React As json object(i am using React and Redux in front end)

Please help me to solve this.. let me know if i can provide any required information...

Edit

My Product Serilizer class

class ProductSerializer < ApplicationSerializer
attributes :id, :slug, :name, :description

has_one :master
has_many :images
has_many :variants
has_many :product_properties
has_many :taxons // Can I change something here.....?

end end

Tryptophan answered 26/5, 2017 at 12:11 Comment(7)
just render it as a json, render json: {taxon: taxon, success: true}Vere
@PraveshKhatri i am getting taxon object already but i need to call a category method while using ARSTryptophan
if you want to call category method from react then you need to call route from react and after completing the manipulation just render it from your category method, like aboveVere
@PraveshKhatri That's also one possible way but what if i want as a product object..Tryptophan
please tell me Prodct.first.category.name how is this is a method. you need to provide more details with your respective controller, routes and modelVere
I am using Spree so you can refer github.com/spree/spree/blob/master/core/app/models/spree/… github.com/spree/spree/blob/master/core/app/models/spree/…Tryptophan
Let us continue this discussion in chat.Vere

© 2022 - 2024 — McMap. All rights reserved.