我的控制器是
class ContactformController < ApplicationController
def new
@message = Message.new
end
def create
@message = Message.new(params[:message])
if @message.valid?
BookingMailer.new_message(@message).deliver
redirect_to(contactform_path, :notice => "Message was successfully sent.")
else
flash.now.alert = "Please fill all fields."
render :new
end
end
end
我的观点是
<div class="row-fluid wiyo_contact_social">
<%= form_for @message, :url => contactform_path do |form| %>
<fieldset class="fields">
<div class="field">
<%= form.label :name %>
<%= form.text_field :name %>
</div>
<div class="field">
<%= form.label :email %>
<%= form.text_field :email %>
</div>
<div class="field">
<%= form.label :subject %>
<%= form.text_field :subject %>
</div>
<div class="field">
<%= form.label :body %>
<%= form.text_area :body %>
</div>
</fieldset>
<fieldset class="actions">
<%= form.submit "Send" %>
</fieldset>
<% end %>
</div>
我的模型是message.rb,数据库表名为“messages”
class Message < ActiveRecord::Base
include ActiveModel::Validations
include ActiveModel::Conversion
extend ActiveModel::Naming
attr_accessor :name, :email, :subject, :body
validates :name, :email, :subject, :body, :presence => true
validates :email, :format => { :with => %r{.+@.+\..+} }, :allow_blank => true
def initialize(attributes = {})
attributes.each do |name, value|
send("#{name}=", value)
end
end
def persisted?
false
end
end
我的配置代码段
match 'contactform' => 'contactform#new', :as => 'contactform', :via => :get
match 'contactform' => 'contactform#create', :as => 'contactpost', :via => :post
当我输入mywebsite.com/contactform时,它为nil提供了错误的未定义方法`[]':NilClass 请帮帮我。
答案 0 :(得分:0)
如果名称,电子邮件,子...字段在数据库中,则不需要它们作为attr_accessor。也没有初始化。 ActiveRecord将为您做到这一点