How to convert a working rails contact form to a boot mod?

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:

 #/app/controllers/contact_controller.rb 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 

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">&times;</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?

+4
source share
1 answer

It seems I was far behind everything!

After the pointer from the #rubyonrails channel, the response was:

1) Copy the form as is, to the modal. (note that probably a different styling is required - but this answer provides the necessary functionality).

2) To add @message = Message.new to the index action on my home_controller.rb

I tried something very similar to this, but had to introduce a different error and thus circumvent the correct solution.

0
source

All Articles