私はactive_attrとsimple_formを使用しています。
フォームを表示する必要があるページに移動すると、次のエラーが表示されます。
undefined method `new_record?' for #<Message body: nil, email: nil, name: nil, subject: nil>
これが私のモデルです:
class Message
include ActiveAttr::Model
attribute :name
attribute :email
attribute :subject
attribute :body
validates :name, :email, :subject, :body, :presence => true
validates :email, :format => { :with => %r{.+@.+\..+} }, :allow_blank => true
end
私のコントローラー:
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
そして最後に私の見解:
<%= simple_form_for @message, :url => contact_path do |f| %>
<%= f.input :name %>
<%= f.input :email %>
<%= f.input :subject %>
<%= f.input :body %>
<%= f.button :wrapped, :submit %>
<% end %>