How to fetch data from table using foreign key using json in rails? -
I have a product table where each product has several images in the image table on the rail pictures, these two tables are on the image table. Foreign keys are connected using ie product_id. Now I want to bring the data from both the tables to the Jason format based on the product ID. What code does my controller need in the show controller? Thank you.
In your controller
def your_function_name @product = products .find (Params [: id]) to search for a product with #product_images = @ product.images response_to do | Format | # JSON for dual functionality as well as HTML format HTML {redirect_to @product} format.json {@product_images} End End
You will also want to see a scene for JSON . For this, you can use gems like jambilar or rabble. For example, if you use jbuilder, you must create a view such as
your_function_name.json.jbuilder
json.extract! @ Images ,: first_varable_of_image, second_team_of_imes, created_it
Comments
Post a Comment