【问题标题】:Rails subscription form sent to an email发送到电子邮件的 Rails 订阅表格
【发布时间】:2017-03-12 01:47:09
【问题描述】:

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

我的问题是在表单提交时发送了电子邮件,但@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

订阅的.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%>

谢谢你帮助我

【问题讨论】:

  • 您正在使用locals 将信息传递给视图。 AFAIK 这些不能通过实例变量获得。尝试删除@new_email上的@
  • 不,我有同样的空电子邮件
  • 好吧,那么您可能在某处打错了字。查看您的日志,您尝试访问的参数的确切名称是什么params[:Email][:email] 看起来很可疑

标签: ruby-on-rails email subscription


【解决方案1】:

好的,所以我找到了有罪的事情:

在 subscribed.html.erb 我需要有 而不是 ... 错字

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-08
    • 2020-03-18
    • 2016-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-11
    相关资源
    最近更新 更多