Action mailer Development Envrionment Rails 3

时间:2023-01-16 10:19:14

So it seems as if I always run into an issue when setting up a mailer in development mode, I cant see anything wrong with my setup, maybe someone else can see something? At the moment when i send an email via my contact form there is no redirect and the params of the email get passed via the url and the same page gets rendered

因此,在开发模式下设置邮件时似乎总是遇到问题,我无法看到我的设置有什么问题,也许其他人可以看到一些东西?当我通过我的联系表单发送电子邮件时,没有重定向,电子邮件的参数通过网址传递,同一页面被渲染

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

Ok so my development environment looks like so

好的,所以我的开发环境看起来像这样

 #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"
}

I have a Contact Controller

我有一个Contact Controller

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

Mailer

信封

class ContactMailer < ActionMailer::Base
 default from: "richlewis14@gmail.com"

 def send_mail(message)
  @message = message
  mail(to: "richlewis14@gmail.com", subject: "Message From Blog Site")
 end
end

Mailer text File

邮件文本文件

<p> You have a new Email</p>

<p><%= @contact.name %></p>
<p><%= @contact.email %></p>
<p><%= @contact.website %></p>
<p><%= @contact.message %></p>

Message model

消息模型

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

And finally my routes

最后我的路线

 #CONTACT FORM
 match 'contact' => 'contact#new', :as => 'contact', :via => :get   
 match 'contact' => 'contact#create', :as => 'contact', :via => :post

UPDATE

UPDATE

Ok so despite suggestions below i cannot get my form to post, it keeps rendering a get request, my form looks like so

好吧,所以尽管下面的建议我无法让我的表格发布,它仍然呈现一个获取请求,我的表格看起来像这样

  <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-->

1 个解决方案

#1


3  

it appears that your form (not shown) is using a GET method, and not matching the create route which is post. Try changing the Form in your view to use POST. and that should invoke the create method. Looking in your logs I am guessing it is executing the new method

您的表单(未显示)似乎正在使用GET方法,并且不匹配发布的创建路由。尝试更改视图中的表单以使用POST。那应该调用create方法。查看您的日志我猜它正在执行新方法

change this.

改变这一点。

 <form id="contact-form">
      <%= form_for @message, :url => contact_path, :method => :post do |f| %>

to this

对此

  <%= form_for @message, :url => contact_path, :method => :post, :html => {:id => 'contact-form' } do |f| %> 

and remove the last </form> as well. since the form_for helper will add it as part of the block. This will remove the nested forms, and should allow you to post / send mail.

并删除最后一个 。因为form_for帮助器会将其添加为块的一部分。这将删除嵌套表单,并允许您发布/发送邮件。

#1


3  

it appears that your form (not shown) is using a GET method, and not matching the create route which is post. Try changing the Form in your view to use POST. and that should invoke the create method. Looking in your logs I am guessing it is executing the new method

您的表单(未显示)似乎正在使用GET方法,并且不匹配发布的创建路由。尝试更改视图中的表单以使用POST。那应该调用create方法。查看您的日志我猜它正在执行新方法

change this.

改变这一点。

 <form id="contact-form">
      <%= form_for @message, :url => contact_path, :method => :post do |f| %>

to this

对此

  <%= form_for @message, :url => contact_path, :method => :post, :html => {:id => 'contact-form' } do |f| %> 

and remove the last </form> as well. since the form_for helper will add it as part of the block. This will remove the nested forms, and should allow you to post / send mail.

并删除最后一个 。因为form_for帮助器会将其添加为块的一部分。这将删除嵌套表单,并允许您发布/发送邮件。