简体   繁体   中英

Action after form Submission on Rails

I have a working form that posts to a controller to send an email.

Controller:

 def contact
    name = params[:contact][:your_name]
    message = params[:contact][:your_message]
    email = params[:contact ][:your_email]
    Contact.contact_form(message, name, email).deliver
    redirect_to :back
 end

Form:

<%= form_for :contact , :url => contact_pages_path(@message), :html => {:method => :put} do |f| %>
<p>
<b>Your email:</b><br>
<%= f.text_field :your_email %>
<b>Name</b><br>
<%= f.text_field :your_name %>
</p>
<p>
<b>Message</b><br>
<%= f.text_area :your_message %>
 </p>
<p>

However I am not very happy with the

redirect_to :back

As it just reloads the page. How can I flash a message up afterwards saying "Thankyou" without moving away from the page? The message could even appear in the form - so afterwards a box appears saying - "Thanks"

redirect_to :back, :notice => "Thank-you"

Of course, this will only work if your application.html.erb template is printing out the contents of notices/alerts. For that, you need something like:

    <% if !notice.nil? || !alert.nil? %>
        <section id="message" class="message-<%= notice.nil? ? "alert" : "notice" %>">
            <div class="row">
                <div>
                    <% if !notice.nil? %>
                        <p class="notice"><%= notice %></p>
                    <% end %>
                    <% if !alert.nil? %>
                        <p class="alert"><%= alert %></p>
                    <% end %>
                </div>
            </div>
        </section>
    <% end %>

I am not sure but could you please try this?

if Contact.contact_form(message, name, email).deliver
  flash[:notice] = "mail has been sent successfully."

I think you may be don't have to redirect to anywhere. Hope it will help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM