最近在看Agile Web Development with Rails第二版,同时在研究rails 2.1。在网上下载了Agile Web Development with Rails第三版的源码,发现第三版对于product全部采用了REST的方式实现了,但是对于cart的实现同第二版没有任何变化,还是放在StoreController中。
class ProductsController < ApplicationController
# GET /products
# GET /products.xml
def index
@products = Product.find(:all)
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @products }
end
end
# GET /products/1
# GET /products/1.xml
def show
@product = Product.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @product }
end
end
# GET /products/new
# GET /products/new.xml
def new
@product = Product.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @product }
end
end
# GET /products/1/edit
def edit
@product = Product.find(params[:id])
end
# POST /products
# POST /products.xml
def create
@product = Product.new(params[:product])
respond_to do |format|
if @product.save
flash[:notice] = 'Product was successfully created.'
format.html { redirect_to(@product) }
format.xml { render :xml => @product, :status => :created, :location => @product }
else
format.html { render :action => "new" }
format.xml { render :xml => @product.errors, :status => :unprocessable_entity }
end
end
end
# PUT /products/1
# PUT /products/1.xml
def update
@product = Product.find(params[:id])
respond_to do |format|
if @product.update_attributes(params[:product])
flash[:notice] = 'Product was successfully updated.'
format.html { redirect_to(@product) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @product.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /products/1
# DELETE /products/1.xml
def destroy
@product = Product.find(params[:id])
@product.destroy
respond_to do |format|
format.html { redirect_to(products_url) }
format.xml { head :ok }
end
end
end
class StoreController < ApplicationController
def add_to_cart
begin
product = Product.find(params[:id])
rescue ActiveRecord::RecordNotFound
logger.error("Attempt to access invalid product #{params[:id]}")
redirect_to_index("Invalid product")
else
@cart = find_cart
@current_item = @cart.add_product(product)
if request.xhr?
respond_to { |format| format.js }
else
redirect_to_index
end
end
end
end
个人觉得/store/add_to_cart/1似乎很不REST,感觉cart应该有个单独的CartController,这样才比较REST。
不知道是否是由于第3版的代码没有完成的而造成的。
分享到:
- 2008-06-20 21:02
- 浏览 886
- 评论(7)
- 论坛回复 / 浏览 (7 / 2429)
- 查看更多
相关推荐
Pragmatic - Agile Web Development with Rails 2ndPragmatic - Agile ... Agile Web Development with Rails 2ndPragmatic - Agile Web Development with Rails 2ndPragmatic - Agile Web Development with Rails 2nd
Agile Web Development with Rails 5 英文无水印原版pdf pdf所有页面使用FoxitReader、PDF-XChangeViewer、SumatraPDF和Firefox测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 查看此书...
Agile Web Development with Rails 2nd Edition
Agile Web Development with Rails 2nd Edition源码
Agile Web Development with Rails (PDF)
Agile Web Development with Rails 2nd PDF格式。
Agile Web Development with Rails
Agile Web Development with Rails 3nd Edition beta
Ruby on Rails helps you produce high-quality, beautiful-looking web applications quickly. You concentrate on creating the application, and Rails takes care of the details., Tens of thousands of ...
Agile Web Development with Rails 1-14节_ppt(老师发的修正版)
agile web development with rails 5(英文电子书).............................................................................................................................................................
Agile Web Development with Rails, 2nd Edition <br>有两份PDF文件,大小分别是7.39MB和6.55MB <br>作者: Dave Thomas , David Heinemeier Hansson , Leon Breedt , Mike Clark , James Duncan Davidson ,...
博文链接:https://jbf034.iteye.com/blog/219935
Agile Web Development with Rails (4th edition).pdf