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?