Rails ContactUs model in single pages controller -
hi have simple rails site no models, used serving static pages.
the site has pages controller, serving 1 home page made of partials.
like such in app/views/pages/home.html.erb
<%= render 'layouts/about' %> <%= render 'layouts/services' %> <%= render 'layouts/rooms' %> <%= render 'layouts/breakfast' %> <%= render 'layouts/guestbook' %> <%= render 'layouts/contact' %>
the home page uses html5/javascript autoscrolls page. each section prefaced # allow auto scrolling section.
<li><a class="home" href="#home">home</a></li> <li><a class="services" href="#services">tarrifs</a></li> <li><a class="portofolio" href="#portofolio">rooms</a></li> <li><a class="breakfast" href="#breakfast">breakfast</a></li> <li><a class="contact" href="#contact" >contact </a></li> <li><a class="services" href="#services">blog / whats on.</a></li> <li><a class="guestbook" href="#guestbook">guest book</a></li> <li class="description">take look.</li>
this works fine. dev site here
now question / issue : i'd add rails type contactus page.
following various tutorials :
they point having message model,actionmailer, , contact controller, works if want have separate contact controller.
i'm stuck @ trying figure how can build contactus form keep pages controller allow autoscrolling work using contact controller seems change url routes. know isn't rails way, great.
hope made sense. ideas ?
i following:
add new model called "form", app/models/form.rb
class form include activemodel::validations include activemodel::conversion extend activemodel::naming attr_accessor :name, :email, :message validates :name, presence: true validates :email, presence: true, email: true validates :message, presence: true def initialize(attributes = {}) attributes.each |name, value| send("#{name}=", value) end end def persisted? false end end
modify pages_controller:
def new @form = form.new end def create @form = form.new(params[:form]) if @form.valid? redirect_to root_path, notice: "thanks!") else render "new" end end
add form html code app/views/pages/home.html.erb
<%= form_for @form |f| %> <div class="field"> <%= f.label :name %> <%= f.text_field :name %> </div> <div class="field"> <%= f.label :email %> <%= f.email_field :email %> </div> <div class="field"> <%= f.label :message %> <%= f.text_area :message %> </div> <div class="actions"> <%= f.submit "submit" %> </div>
and that´s it, have build in contact form.
this code collected 1 of gems on github: https://github.com/mattherick/contact_form
for testing can add gem gemfile , run files generators, described in github readme.