I currently have a working rails contact form (taken from another SO answer) available in / contact, which uses a controller, model, and mailer (Mandrill). It reacts to new ones and creates routes in the controller and still uses activemodel functions. The form does not use ajax.
Now I need to try to create my contact form on the home page, as opposed to the page / contact using a modal popup.
I read a few SO queries about modules, but everything seems to be related to getting a modality to do something specific - and not so much to creating a modal form to mimic a normal form.
To get started, I added a working modal to the home page.
When I then try to add the form to the homepage, I launch method errors, since the form is part of the home_controller. After copying my new ones and creating controller actions to my home controller, I realized that the form is just hidden and is still executed when the page loads (by the action of the index). Adding @message = Message.new to the index action does not seem to help - and do I want this for a modal load?
Here are the main moving parts of my working form from the contact page and the working modal block from the main page - how can I combine these two and keep my existing functionality?
here is the contact controller:
message model
#app/models/message.rb class Message 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
And the view:
<%= form_for @message, :url => contact_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 %>
Finally, on my homepage, I have this for a modal (formless)
<div class="modal fade" id="modal_contact_form"> <div class="modal-header"> <a class="close" data-dismiss="modal">×</a> <h3>Contact Form</h3> </div> <div class="modal-body"> <p>Test Content for Modal</p> </div> <div class="modal-footer"> </div> </div>
which is called by this <li><a href="#modal_contact_form" data-toggle="modal">contact</a></li>
My attempts were numerous and varied to bring this work to this point - the main problem I need for my head (I think) is that the form is loaded by the action of the index on the home_controller. Ideally, I assume I still want to use the logic in my contact_controller?
Any advice or suggestions appreciated - for now, I wonder if there is an easy way to just move the form to modal or not!
Greetings
Mitzpa
note: In response to the requests I have about ajax, I avoided ajax due to perceived difficulty and lack of progress when trying to use it on rails 3.2. I would be happy to use it if in fact it would be easier to do this in aaaxified form?