A website template with lots of features, built with ruby on rails.

subscription_controller.rb 1.1KB

12345678910111213141516171819202122232425262728
  1. class SubscriptionController < ApplicationController
  2. def create
  3. @subscription = Subscription.new(subscription_params)
  4. respond_to do |format|
  5. if Subscription.find_by_email(@subscription.email) == nil
  6. if @subscription.save
  7. UserMailer.newsletter_subscription(@subscription).deliver
  8. format.html { redirect_to root_path, notice: 'Thanks for subscribing to our newsletter' }
  9. format.json { render action: 'show', status: :created, location: @subscription }
  10. else
  11. format.html { redirect_to root_path, alert: 'An error ocured. Please try gain.' }
  12. format.json { render json: @subscription.errors, status: :unprocessable_entity }
  13. end
  14. else
  15. format.html { redirect_to root_path, alert: 'You have already registered to our newsletter' }
  16. format.json { render action: 'show', status: :created, location: @subscription }
  17. end
  18. end
  19. end
  20. # Never trust parameters from the scary internet, only allow the white list through.
  21. def subscription_params
  22. params.require(:subscription).permit(:first_name, :last_name, :email)
  23. end
  24. end