[英]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. 当然,这仅在您的
application.html.erb
模板正在打印通知/警报的内容时才有效。 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.
希望它会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.