I am trying to implement a contact us form on an existing view (the "show" view of a model).
I get a undefined method `model_name' for NilClass:Class and I'm not sure if I'm implementing things correctly.
In my show.html.erb
<%= form_for @message do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.label :email %><br />
<%= f.text_field :email %>
</p>
<p>
<%= f.label :content, "Message" %><br />
<%= f.text_area :content %>
</p>
<p><%= f.submit "Send Message" %></p>
<% end %>
In my main controller that has all the actions for the site (index, update, show), I added the following actions
# GET /listings/1
# GET /listings/1.json
def show
@listing = Listing.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @listing }
end
end
def newmessage
@message = Message.new
end
def sendmessage
@message = Message.new(params[:message])
if @message.valid?
# MessageMailer.new_message(@message).deliver
flash[:notice] = "Message envoy avec succes"
redirect_to root_path
else
render :newmessage
end
end
And my Message.rb class looks like this:
class Message
extend ActiveModel::Naming
include ActiveModel::Conversion
include ActiveModel::Validations
include ActionView::Helpers::TextHelper
attr_accessor :name, :email, :message
validates :name,
:presence => true
validates :email,
:format => { :with => /\b[A-Z0-9._%a-z\-]+@(?:[A-Z0-9a-z\-]+\.)+[A-Za-z]{2,4}\z/ }
validates :message,
:length => { :minimum => 10, :maximum => 1000 }
def initialize(attributes = {})
attributes.each do |name, value|
send("#{name}=", value)
end
end
def persisted?
false
end
end