route
resources :line_items
resources :carts
resources :sites
root :to => 'store#index', :as => 'store'
resources :products
###line_items_controller.rb
def create
@cart = current_cart
product = Product.find(params[:product_id])
@line_item = @cart.add_product(product.id)
respond_to do |format|
if @line_item.save
format.html { redirect_to(@line_item.cart) }
format.json { render :show, status: :created, location: @line_item }
else
format.html { render :new }
format.json { render json: @line_item.errors, status: :unprocessable_entity }
end
end
end
###store.index.erb
<h1>Your Pragmatic Catalog</h1>
<% @products.each do |product| %>
<div class="entry">
<%= link_to image_tag(product.image_url),line_items_path(:product_id => product),
method: :post %><br />
<h3><%= product.title %></h3>
<%= sanitize(product.description) %>
<div class="price_line">
<span class="price"><%= number_to_currency(product.price) %></span>
<%= button_to 'Add to Cat',line_items_path(:product_id => product )%>
</div>
</div>
<% end %>
点击图片之后。却并没有实现添加到购物车这个功能。而且 redirect 的页面也不对。请问哪里有问题啊?