我是新来的轨道。当我阅读任务 G 时,请查看使用 Rails 进行敏捷 Web 开发的部分。我遇到了一个非常奇怪的问题。
/home/chenhao/ruby/depot/app/views/orders/_form.html.erb 其中第 1 行提出:
NilClass:Class 的未定义方法“model_name”
我猜 nil @order 会导致这个错误,但我已经在控制器的新方法中初始化了 @order。谁能帮我解决这个奇怪的错误?
这是_form.html.erb
<%= form_for(@order) do |f| %>
<% if @order.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@order.errors.count, "error") %> prohibited this order from being saved:</h2>
<ul>
<% @order.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name, :size=>40 %>
</div>
<div class="field">
<%= f.label :address %><br />
<%= f.text_area :address, :row=>3, :col=>40 %>
</div>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, :size=>40 %>
</div>
<div class="field">
<%= f.label :pay_type %><br />
<%= f.select :pay_type, Order::PAYMENT_TYPES, :prompt=>"Select a payment method" %>
</div>
<div class="actions">
<%= f.submit "Place Order"%>
</div>
<% end %>
这是控制器
class OrdersController < ApplicationController
# GET /orders
# GET /orders.json
def index
@orders = Order.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @orders }
end
end
# GET /orders/1
# GET /orders/1.json
def show
@order = Order.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @order }
end
end
# GET /orders/new
# GET /orders/new.json
def new
@cart = current_cart
if @cart.line_items.empty?
redirect_to store_url, :notice => 'Your cart is empty'
return
end
@order = Order.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @order }
end
end
# GET /orders/1/edit
def edit
@order = Order.find(params[:id])
end
# POST /orders
# POST /orders.json
def create
@order = Order.new(params[:order])
@order.add_line_items_from_cart(current_cart)
respond_to do |format|
if @order.save
Cart.destroy(session[:cart_id])
session[:cart_id] = nil
format.html { redirect_to store_url, notice: 'Thank you for your order.' }
format.json { render json: @order, status: :created, location: @order }
else
@cart = current_cart
format.html { render action: "new" }
format.json { render json: @order.errors, status: :unprocessable_entity }
end
end
end
# PUT /orders/1
# PUT /orders/1.json
def update
@order = Order.find(params[:id])
respond_to do |format|
if @order.update_attributes(params[:order])
format.html { redirect_to @order, notice: 'Order was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @order.errors, status: :unprocessable_entity }
end
end
end
# DELETE /orders/1
# DELETE /orders/1.json
def destroy
@order = Order.find(params[:id])
@order.destroy
respond_to do |format|
format.html { redirect_to orders_url }
format.json { head :no_content }
end
end
end
和模型:
class Order < ActiveRecord::Base
has_many :line_items, :dependent => :destory
attr_accessible :address, :email, :name, :pay_type
PAYMENT_TYPES = ["Check", "Credit card","Purchase Order"]
validates :address, :email, :name, :presence => true
validates :pay_type, :inclusion => PAYMENT_TYPES
def add_line_items_from_cart(cart)
cart.line_items.each do |item|
item.cart_id = nil
line_items << item
end
end
end