Rails 订阅表格已发送至邮箱

Rails subscription form sent to an email

我正在尝试创建一个订阅表格以提前访问我的平台,但我想不通。我使用的表格应该使用订阅者的电子邮件将一封电子邮件发送到另一封电子邮件,仅此而已。

我的问题是电子邮件是在表单提交时发送的,但是@new_email 每次都是空的。

welcome_controller.rb:

class WelcomeController < ApplicationController

  def home

  end

  def about
  end

  def subscribe


    WelcomeMailer.subscribed(params[:Email][:email]).deliver
    flash[:success] = "We've got your request, we'll be in touch"
    redirect_to root_path

  end
end

Subscribed.html.erb

<!DOCTYPE html>
<html>
<head>
    <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
</head>
<body>
    "You have a new subscriber!
    Email : <% @new_email %>

    Bye"
</body>
</html>

welcome_mailer.rb

class WelcomeMailer < ActionMailer::Base
    default :from => "mail@mail.com"

    def subscribed(params)
        mail(:to => "myname@gmail.com", :subject => "We've got a new subscriber") do |format|
            format.html {
                render locals: { new_email: params }
            }
        end
    end
end

我的表格:

    <%= form_tag :subscribe, :url => {:controller => 'welcome', :action => 'subscribe'}, class:'navbar-form navbar-center',onsubmit: "return continueOrNot()"  do %>
        <div class="form-group">
                <%= text_field 'Email','email', placeholder: "Enter email", class:'form-control',required: 'true'%>

                <%= submit_tag 'Sign-in me in',:id=>'submit',class:'btn btn-info btn-xl' %>
                <br/><small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
        </div>
    <%end%>

谢谢你帮助我

好的,所以我找到了罪魁祸首:

在 subscribed.html.erb 我需要 <%= new_email %> 而不是 <% new_email %> ... 打字错误

@phoet 你是对的。此外,我已从 text_field 切换到 text_field_tag 以获得更多 name/id 控制。