Caleb Sanchez
Caleb Sanchez

Reputation: 3

How can I push an element into a session array when i click a link in my ruby view

I am new to programming in ruby on rails and ruby in general. The website I have created has two models, categories and products, where categories has_many products. The user goes through the categories and picks products and adds them to a cart to buy. I have session[:cart] that will contain the id numbers of each product the user picks. I have the controller and view set up for my cart so that it will show all the products that user picks and totals the price of the cart. There is a link on every "show" view for every product that shows all the values, like name and price, for that product. on that page there is a link that says "ADD TO CART"to the cart page.

My problem is how do i push that product id into the session[:cart] when the i click on the link? do i use a button instead or a post method somehow? i am lost here.

Upvotes: 0

Views: 246

Answers (1)

Ahmad Hussain
Ahmad Hussain

Reputation: 2491

In my opinion you need to use cookies for that because cookies can store more data then session and it store in you browser so you can set them using javascript.

And to add into cart you need to store only ids of products and when every you want to show result just show result on the bases of ids and you can store ids comma separated.

Upvotes: 1

Related Questions