我想添加一個聯繫表格爲我的Rails 3.1.3應用程序使用this tutorial。然而,最後當我嘗試加載我的聯繫頁面時,出現錯誤:沒有對象,當你不希望它的聯繫form_for
當你沒有想到它時,你有一個零對象! 您可能預期了Array的一個實例。在評估零發生 錯誤[]
它說,它發生在new.html.haml頁面上該代碼塊的1行:
= form_for @message, :url => { :action=>"new", :controller=>"contact"} do |form|
%fieldset.fields
.field
= form.label :name
= form.text_field :name
.field
= form.label :email
= form.text_field :email
.field
= form.label :body
= form.text_area :body
%fieldset.actions
= form.submit "Send"
我的控制器看起來是這樣的:
class ContactController < ApplicationController
def new
@message = Message.new
end
def create
@message = Message.new(params[:message])
if @message.valid?
NotificationsMailer.new_message(@message).deliver
redirect_to(root_path, :notice => "Message was successfully sent.")
else
flash.now.alert = "Please fill all fields."
render :new
end
end
end
模式是這樣的:
class Message < ActiveRecord::Base
include ActiveModel::Validations
include ActiveModel::Conversion
extend ActiveModel::Naming
attr_accessor :name, :email, :body
validates :name, :email, :body, :presence => true
validates :email, :format => { :with => %r{[email protected]+\..+} }, :allow_blank => true
def initialize(attributes = {})
attributes.each do |name, value|
send("#{name}=", value)
end
end
def persisted?
false
end
end
瓦我會得到那個錯誤,我該如何解決它?謝謝!
是您的HAML縮進正確上面鍵入?它看起來像'%fieldset.fields'不是'form_for'的子代 –
是的,那只是一個複製/粘貼錯誤。據我所知,所有內容都被正確縮進。 – Anoel