因此,似乎我在開發模式下設置郵件程序時總是遇到問題,我無法看到設置有任何問題,也許其他人可以看到某些內容?在那一刻,我通過我的聯繫形式發送電子郵件沒有重定向和電子郵件的PARAMS通過URL獲得通過,並在同一頁面獲取呈現動作郵件程序開發環境軌跡3
http://localhost:3000/contact?utf8=%E2%9C%93&authenticity_token=123456exampletoken%3D&message%5Bname%5D=richard+lewis&message%5Bemail%5D=richlewis14%40gmail.com&message%5Bwebsite%5D=bbc.co.uk&message%5Bmessage%5D=test%0D%0A&commit=Send+Message
好了,所以我的開發環境看起來像這樣
#Mailer Config
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
config.action_mailer.default_url_options = { host: "localhost:3000" }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: 587,
domain: "gmail.com",
authentication: "plain",
enable_starttls_auto: true,
user_name: "myusername",
password: "mypassword"
}
我有聯繫控制器
class ContactController < ApplicationController
def new
@message = Message.new
end
def create
@message = Message.new(params[:message])
if @message.valid?
ContactMailer.send_mail(@message).deliver
redirect_to(root_path, :notice => "Thanks for your message, I will be in touch soon")
else
render :new
end
end
end
梅勒
class ContactMailer < ActionMailer::Base
default from: "[email protected]"
def send_mail(message)
@message = message
mail(to: "[email protected]", subject: "Message From Blog Site")
end
end
梅勒文本文件
<p> You have a new Email</p>
<p><%= @contact.name %></p>
<p><%= @contact.email %></p>
<p><%= @contact.website %></p>
<p><%= @contact.message %></p>
消息模型
class Message
include ActiveModel::Validations
include ActiveModel::Conversion
extend ActiveModel::Naming
attr_accessor :name, :email, :website, :message
def initialize(attributes = {})
attributes.each do |name, value|
send("#{name}=", value)
end
end
def persisted?
false
end
end
最後我的路線
#CONTACT FORM
match 'contact' => 'contact#new', :as => 'contact', :via => :get
match 'contact' => 'contact#create', :as => 'contact', :via => :post
UPDATE
好了,所以儘管下面我建議,不能讓我的形式發佈,它可以EPS渲染GET請求,我的形式看起來像這樣
<div id="contact-form-wrap">
<form id="contact-form">
<%= form_for @message, :url => contact_path, :method => :post do |f| %>
<span class="c-note">Asunt in anim uis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in anim id est laborum. Allamco laboris nisi ut aliquip ex ea commodo consequat. Aser velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint</span>
<div class="contact-left">
<p class="input-block clearfix">
<%= f.text_field :name, :id => 'contact_name', :placeholder => 'Name' %>
</p>
<p class="input-block">
<%= f.text_field :email, :id => 'contact_email', :placeholder => 'Email' %>
</p>
<p class="input-block last">
<%= f.text_field :website, :id => 'contact_url', :placeholder => 'Website' %>
</p>
</div><!--end:contact-left-->
<div class="contact-right">
<p class="textarea-block">
<%= f.text_area :message, :id => 'contact_message', :rows => 6, :placeholder => 'Message' %>
</p>
</div><!--end:contact-right-->
<div class="clear"></div>
<p class="contact-button clearfix">
<%= f.submit 'Send Message', :id => 'submit-contact' %>
</p>
<div class="clear"></div>
<% end %>
</form>
</div><!--contact-form-wrap-->
你能收到郵件嗎? 只是在服務器上看到。 –
您可以在config/routes.rb.中設置'resources:contact',默認導軌將處理請求。請刪除當前添加的行在我們的配置/ routes.rb – Amit
嘗試,但後來我找不到動作索引,我應該只使用[:new,:create] – Richlewis