I am currently trying to create a shopping cart for my rails application and once populated my cart should be saved in the session.
Everytime i try to run the server to test if working or not, it seems my routes are not working at all...
See below my routes.rb, my cart controller and my index.
Any help will be appreciated.
routes.rb
Sewingsupplies::Application.routes.draw do
get "cart/index"
get "cart/success"
PUT "/cart/:id" => "cart#add"
DELETE "/cart/:id" => "cart#remove"
DELETE "/cart" => "cart#clear"
POST "/cart/checkout"
devise_for :admins
resources :catalogues
devise_for :users
cart controller
def index
sessions[:cart] = {}
render 'cart/index'
end
def success
end
def add
@catalogue = Catalogue.find(params[:id])
cart = sessions[:cart]
cart = {:catalogue => @catalogue.name,category => @catalogue.category, :code => @catalogue.code , :colour=> @catalogue.colour, :description => @catalogue.description, :image => @catalogue.image , :unitprice => += @catalogue.unitprice, :unitquantity => +=1, :unitweight => += @catalogue.unitweight }
sessions[:cart] = cart
render 'cart/add'
end
end
and my index page
<h1>Listing catalogues</h1>
<table>
<tr>
<th>Name</th>
<th>Code</th>
<th>Category</th>
<th>Description</th>
<th>Unitprice</th>
<th>Unitquantity</th>
<th>Unitweight</th>
<th>Colour</th>
<th>Image</th>
<th>User</th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
<% @catalogues.each do |catalogue| %>
<tr>
<td><%= catalogue.name %></td>
<td><%= catalogue.code %></td>
<td><%= catalogue.category %></td>
<td><%= catalogue.description %></td>
<td><%= catalogue.unitprice %></td>
<td><%= catalogue.unitquantity %></td>
<td><%= catalogue.unitweight %></td>
<td><%= catalogue.colour %></td>
<td><%= image_tag(catalogue.image, :width => 150) if catalogue.image.present?%></td>
<td><%= catalogue.user.email %></td>
<td><%= link_to 'Show', catalogue %></td>
<td><%= link_to 'Edit', edit_catalogue_path(catalogue) %></td>
<td><%= link_to 'Destroy', catalogue, method: :delete, data: { confirm: 'Are you sure?' } %></td>
<td><%= link_to "Add to cart", controller: "cart", action: "add", id: @catalogue.id, method: :post %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to "View cart", controller: "cart", action: "index" %>
<%= link_to 'New Catalogue', new_catalogue_path %>
In your routes.rb file you have defined the routes with PUT and DELETE in uppercase. Define the routes in lowercase like put and delete as these are methods of ActionDispatch::Routing::Mapper class which are called to setup routes for your application. As ruby is case sensitive you need to call the method with correct case.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments