zoukankan      html  css  js  c++  java
  • 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。

  • 相关阅读:
    js-21点小游戏
    js-打印出现最多次的字母
    盒模型浮动
    九九乘法表
    猫眼-湄公河行动电影介绍页面
    (day4)用css画三角形以及红旗
    cookie的使用
    用Servlet校验密码2
    Servlet登录验证
    Servlet概述
  • 原文地址:https://www.cnblogs.com/ToDoToTry/p/2125101.html
Copyright © 2011-2022 走看看