0

I am new to rails and I have an application that is supposed to send a confirmation email after creating a booking however, when I go to the /letter_opener tab after creating a booking nothing shows up

application_mailer.rb

class ApplicationMailer < ActionMailer::Base
default from: "support@flightbooker.com"
layout "mailer"
end

passenger_mailer.rb

class PassengerMailer < ApplicationMailer

 def booking_confirmation
    @passenger = params[:passenger]
    @booking = params[:booking]
    @flight = params[:flight]

    mail(to: @passenger.email, subject: "Confirmation Email")
end

end

bookings_controller.rb

 class BookingsController < ApplicationController

 include BookingsHelper

 def new
    @booking = Booking.new
    @selected_flight = Flight.find(params[:flight])
    @passengers_count = params[:passengers].to_i
    @passengers_count.times{@booking.passengers.build}
 end

 def create

    @booking = Booking.new(booking_params)

    if @booking.save

        
    @booking.passengers.each do |pax|
        PassengerMailer.with(passenger: pax).booking_confirmation.deliver_now
      end
      
        flash[:success] = 'Booking saved!'
        redirect_to booking_path(@booking)
    else
        flash[:alert] = "An error has occured!"
        render "new"
    end
 end

 def show

    @booking = Booking.find(params[:id])
 end





end

Gemfile

 source "https://rubygems.org"
 git_source(:github) { |repo| "https://github.com/#{repo}.git" }

 ruby "3.1.2"

 # Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main"
 gem "rails", "~> 7.0.4"

 # The original asset pipeline for Rails [https://github.com/rails/sprockets-rails]
 gem "sprockets-rails"

 # Use sqlite3 as the database for Active Record
 gem "sqlite3", "~> 1.4"

 # Use the Puma web server [https://github.com/puma/puma]
 gem "puma", "~> 5.0"

 # Use JavaScript with ESM import maps [https://github.com/rails/importmap-rails]
 gem "importmap-rails"

# Hotwire's SPA-like page accelerator [https://turbo.hotwired.dev]
gem "turbo-rails"

# Hotwire's modest JavaScript framework [https://stimulus.hotwired.dev]
gem "stimulus-rails"

# Build JSON APIs with ease [https://github.com/rails/jbuilder]
gem "jbuilder"

# Use Redis adapter to run Action Cable in production
# gem "redis", "~> 4.0"

# Use Kredis to get higher-level data types in Redis 
[https://github.com/rails/kredis]
# gem "kredis"

# Use Active Model has_secure_password 
[https://guides.rubyonrails.org/active_model_basics.html#securepassword]
# gem "bcrypt", "~> 3.1.7"

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem "tzinfo-data", platforms: %i[ mingw mswin x64_mingw jruby ]

# Reduces boot times through caching; required in config/boot.rb
gem "bootsnap", require: false

# Use Sass to process CSS
# gem "sassc-rails"

# Use Active Storage variants 
[https://guides.rubyonrails.org/active_storage_overview.html#transforming-images]
# gem "image_processing", "~> 1.2"

group :development, :test do

See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-

the-debug-gem
gem "debug", platforms: %i[ mri mingw x64_mingw ]
end

group :development do
  # Use console on exceptions pages [https://github.com/rails/web-console]
 gem "web-console"

  # Add speed badges [https://github.com/MiniProfiler/rack-mini-profiler]
  # gem "rack-mini-profiler"

  # Speed up commands on slow machines / big apps [https://github.com/rails/spring]
  # gem "spring"

  gem 'letter_opener_web'

end

group :test do
  # Use system testing [https://guides.rubyonrails.org/testing.html#system-testing]
  gem "capybara"
  gem "selenium-webdriver"
  gem "webdrivers"

end

development.rb

require "active_support/core_ext/integer/time"

Rails.application.configure do
  # Settings specified here will take precedence over those in config/application.rb.


  # In the development environment your application's code is reloaded any time
  # it changes. This slows down response time but is perfect for development
  # since you don't have to restart the web server when you make code changes.
  config.cache_classes = false

  # Do not eager load code on boot.
  config.eager_load = false

  # Show full error reports.
  config.consider_all_requests_local = true

  # Enable server timing
  config.server_timing = true

  # Enable/disable caching. By default caching is disabled.
  # Run rails dev:cache to toggle caching.
     if Rails.root.join("tmp/caching-dev.txt").exist?
    config.action_controller.perform_caching = true
    config.action_controller.enable_fragment_cache_logging = true

    config.cache_store = :memory_store
    config.public_file_server.headers = {
      "Cache-Control" => "public, max-age=#{2.days.to_i}"
     }
  else
    config.action_controller.perform_caching = false

    config.cache_store = :null_store
  end

  # Store uploaded files on the local file system (see config/storage.yml for 
options).
  config.active_storage.service = :local

  # Don't care if the mailer can't send.
  config.action_mailer.raise_delivery_errors = true

  config.action_mailer.delivery_method = :letter_opener
  config.action_mailer.perform_deliveries = true


  config.action_mailer.perform_caching = false

  # Print deprecation notices to the Rails logger.
  config.active_support.deprecation = :log

  # Raise exceptions for disallowed deprecations.
  config.active_support.disallowed_deprecation = :raise

  # Tell Active Support which deprecation messages to disallow.
  config.active_support.disallowed_deprecation_warnings = []

  # Raise an error on page load if there are pending migrations.
  config.active_record.migration_error = :page_load

  # Highlight code that triggered database queries in logs.
   config.active_record.verbose_query_logs = true

 # Suppress logger output for asset requests.
  config.assets.quiet = true

  # Raises error for missing translations.
  # config.i18n.raise_on_missing_translations = true

  # Annotate rendered view with file names.
  # config.action_view.annotate_rendered_view_with_filenames = true

  # Uncomment if you wish to allow Action Cable access from any origin.
  # config.action_cable.disable_request_forgery_protection = true



end

routes.rb

Rails.application.routes.draw do
  # Define your application routes per the DSL in 
https://guides.rubyonrails.org/routing.html

  # Defines the root path route ("/")
  root "flights#index"

  resources :flights
  resources :airports 
  resources :bookings

  mount LetterOpenerWeb::Engine, at: "/letter_opener" if Rails.env.development?
end

if you still need more code here is a link to my github https://github.com/Michael-Mehta/odin-flight-booker

CoolGuy
  • 99
  • 7
  • "nothing happens" give us nothing to work with other than guesses. Thank you for including lots of code (most people leave way too much out) but to troubleshoot something like this you use the console. So add to your question the console output from when you create the booking to requesting the next page. What is your server doing or NOT doing? – Beartech Dec 08 '22 at 06:02
  • Also in your bookings_controller.rb try changing `.deliver_now` to `deliver!` as that will raise any errors and look in the console for those errors. – Beartech Dec 08 '22 at 06:31

0 Answers0