I am developing an ecommerce app and I have a CSV export feature which exports all product details like name, price etc. Does. Each product is in line with columns for each product attribute I want to add a column to the file that contains the URL of each product. The reason for this is that I can use it as a product feed which can be deposited on various shopping sites.
Here is my export code in controller. How do I add a column called Root to do this? I have no root column in the model.
#controller def Product List @ listing = listings. All respond_to do | Format | Format.html format.csv {send_data @ listings.to_csv (@listings)} End and # model F. self.to_csv (listing) wanted_columns = [: sku ,: name ,: designer_or_brand,: description, price,: saleprice ,: List, category: CSV.generate do | Csv | CSV & LT; & Lt; ['Product_ID', 'Product_title', 'Designer_or_Brand', 'Description', 'Price', 'SalePrice', 'Quantity_in_stock', 'Category'] + [: Image, Image2, Image3, Image4] listings.each List | Attrs = listing.attributes.with_indifferent_access.values_at (* wanted_columns) attrs.push (listing.image.url, listing.image2.try (url), listing. Image3.try (: url), listing.image4.try ( : Url) CSV & lt; & Lt; Attrs end end end
def self.to_csv (listing) wanted_columns = [: Sku ,: name ,: designer_or_brand,: description ,: Price ,: saleprice ,: inventory ,: category] header =% w (product_id product_title designer_corebrid details price sale price quantity_in_stock category image image2 image3 image4 productUrl) CSV.generate do | Csv | CSV & LT; & Lt; Top listings.each do | Listing | Attrs = listing.attributes.with_indifferent_access.values_at (* wanted_columns) & lt; & Lt; Listing.image.url & lt; & Lt; Listing.image2.try (: url) & lt; & Lt; Listing.image3.try (: url) & lt; & Lt; Listing.image4.try (: url) & lt; & Lt; Rails.application.routes.url_helpers.product_url (listing.Product_ID) CSV & lt; & Lt; Attrs end end end
In fact, only the ultimate object of the difference array is:
Rails.application.routes.url_helpers.product_url (listing.Product_ID)
, whereProduct_url
Your code isProduct # Show
Comments
Post a Comment