Search code examples
ruby-on-railsemailsubscription

Rails subscription form sent to an email


I'm trying to create a subscription form for early access to my platform but I can't figure something. The form I use should send an email to another email with the subscriber's email and that's all.

My issue is that the email is sent at form submit, but @new_email is empty every time.

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

And my form:

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

Thanks for helping me


Solution

  • Ok so I've found the guilty thing:

    In subscribed.html.erb I need to have <%= new_email %> instead of <% new_email %> ... typo

    @phoet you were right. Also I've switched from text_field to text_field_tag for more name/id control.