rails NoMethodError in controller 解决

来源:转载

NoMethodError in ProductsController#create

undefined method `tittle' for #<Product:0xb67f6fdc>

Rails.root: /home/fandy/RailsCode/depot

Application Trace | Framework Trace | Full Trace
app/controllers/products_controller.rb:46:in `create'app/controllers/products_controller.rb:45:in `create'
class ProductsController < ApplicationController

# GET /products

# GET /products.xml

def index

@products = Product.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
format.html { redirect_to(@product, :notice => 'Product was successfully created.') }
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])
format.html { redirect_to(@product, :notice => 'Product was successfully updated.') }
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

思索半天 才想起来,create action 会用到 model, 而我在 model 里面添加了validate验证。

打开一看果然,:title 被我写成了:tittle

---------------------------------------

rails在@product.save, orm 前会执行 model里面的 validate。


分享给朋友:
您可能感兴趣的文章:
随机阅读: