0

I have the following challenge: I use a contact form (whích is working) but I need another, different contact form on a different controller and view.

What I have so far:

#messages_controller.rb    
class MessagesController < ApplicationController
        def new
        @message = Message.new
      end

      def create
        @message = Message.new(message_params)

        if @message.valid?
          UserMailer.new_message(@message).deliver
          redirect_to contact_path, notice: "Deine Nachricht wurde erfolgreich versendet. Wir melden uns schnellstmöglich bei Dir."
        else
          flash[:alert] = "Immer diese Technik! Die Nachricht konnte au unerklärlichen Gründen nicht gesendet werden. Bitte versuche es noch einmal."
          render :new
        end
      end

    private

      def message_params
        params.require(:message).permit(:name, :email, :content)
      end
    end

In my view:

#views/messages/new.html.erb
<%= form_for @message, url: contact_path do |f| %>
<%= render 'shared/error_messages', object: f.object %>
  <div class="jumbotron">
    <div class="jumbotron-contents">
      <h5>Du hast Fragen oder Anmerkungen? Schicke uns eine Nachricht!</h5>
      <p>Wir freuen uns von Dir zu hören und stehen Dir bei jedem Deiner Anliegen zur Seite. Unser Team antwortet Dir so schnell wie möglich, normalerweise innerhalb eines Arbeitstages.</p>
      <hr>
      <div class="form-group">
        <%= f.label :name, "Dein Name" %>
        <%= f.text_field :name, class: 'form-control' %>
      </div>

      <div class="form-group">
        <%= f.label :email, "Deine Email" %>
        <%= f.email_field :email, class: 'form-control' %>
      </div>

      <div class="form-group">
          <%= f.label :content, "Deine Nachricht" %>
        <%= f.text_area :content, class: 'form-control' %>
      </div>
      <%= f.submit "Nachricht senden", class: "btn btn-block btn-primary" %>
    </div>
    </div>
<% end %>

As a first try I copied the form into another view "listing#show", initialized a new message object and I was able to use the same form. After sending the form I end up on the contact URL (which is also not exactly what I want)

Here my routes for contact

get 'kontakt', to: 'messages#new', as: 'contact'
post 'kontakt', to: 'messages#create'

But now I am kind of stuck on how to change the form so that:

  • it has different form elements (e.g. additional phone number)
  • after sending I stay on my current site
  • using a different sending method

I tried to create another method in the listing controller named "inquiry" and all, but that doesn't get me any further. How are you handling many different contact forms? Is my basic setup correct for that purpose?

Freebian
  • 107
  • 11

1 Answers1

0

You can make different controllers/models for different types of forms. If you like you can share code by inheriting from the same FormController and by using partials in your views.

Perhaps a generic contact form and a specific request inquiry form as an example. Both will have their own controller:

class ContactFormController < FormController
class InquiryFormController < FormController

class FormController < ApplicationController
  # contains generic form code

In these you can use form_tag to build up the forms (you don't need a model for this per se):

views/contact_form/show.html.erb
views/inquiry_form/show.html.erb

(If you prefer, you can always make a model for a ContactForm and an InquiryForm.)

Routes you can build up like this

get 'kontakt', to: 'contact_form#new'
post 'kontakt', to: 'contact_form#create'

get 'inquiry', to: 'inquiry_form#new'
post 'inquiry', to: 'inquiry_form#create'

Hope this helps.

sourcx
  • 944
  • 7
  • 22
  • Is there also a possibility to integrate it into my existing listing controller? With you suggested approach the user will also jump to the inquiry form URL after sending the form. I would prefer that the user stays on the listing page and gets a notification that the inquiry was sent – Freebian Sep 01 '15 at 09:53
  • Of course you can also integrate it into one controller but you can also `redirect_to` the required page at the end of your controller action (see http://api.rubyonrails.org/classes/ActionController/Redirecting.html). Integrating it in one controller probably means you will have different types of Message models. You can solve this by subclassing the Message model or adding some properties to it so that you can identify the type of message (like message.is_inquiry?) – sourcx Sep 01 '15 at 09:59
  • Sorry for asking again, but I am new to Ruby. Since I need to send the current listing name/id with the email form I think it is better to integrate this into the same controller? Can I use the #show method inside the listing controller and enhance it by something like if params[message] or something? Kind of reloading the same page but sending the email details... – Freebian Sep 01 '15 at 10:16
  • No worries! I don't understand how MessagesController is related to your listing. Do you have another view for listings as well? (can you add them to your question if so). You can render a listing page and then also render a contact form on this same page (perhaps with a partial) that gets submitted to the ContactForm controller. The ContactForm controller method (create/send or something) can then redirect back to the listing page after sending a message). – sourcx Sep 01 '15 at 11:44
  • Hey, yes I did as you said. Integrated the form in my listing view, wrote a new method in controller called "inquiry", set the routes accordingly and created the corresponding mail template. The only thing though, is that if I submit an empty form the errors won't show up due to my redirect_to request.referrer. – Freebian Sep 01 '15 at 13:08
  • perhaps this question can help you with this: http://stackoverflow.com/questions/7510418/rails-redirect-to-with-error-but-flasherror-empty – sourcx Sep 02 '15 at 13:01