2

I have been following quite an old tutorial in order to create a contact form and then create a mailer that sends to the site admin. I am running into some problems i think because i am on rails 4.1.1 and the tutorial is so old! I was wondering if anyone could give me some guidance-i am learning rails. The error i am currently getting is NoMethodError in MessagesController#create undefined method `subject' for # subject message.subject

here is my code

messages controller:

class MessagesController < ApplicationController
  before_action :set_message, only: [:show, :edit, :update, :destroy]

  # GET /messages
  # GET /messages.json
  def index
    @messages = Message.all
  end

  # GET /messages/1
  # GET /messages/1.json
  def show
  end

  # GET /messages/new
  def new
    @message = Message.new
  end

  # GET /messages/1/edit
  def edit
  end

  # POST /messages
  # POST /messages.json
  def create
    @message = Message.new(message_params)

respond_to do |format|
  if @message.save
    ContactMailer.deliver_message(@message)
    flash.now[:notice] = 'Thank you for your message!'
    format.html { redirect_to root_path }
    format.json { render :show, status: :created, location: @message }
  else
    format.html { render :new }
    format.json { render json: @message.errors, status: :unprocessable_entity }
  end
end

end

# PATCH/PUT /messages/1 # PATCH/PUT /messages/1.json def update respond_to do |format| if @message.update(message_params) format.html { redirect_to @message, notice: 'Message was successfully updated.' } format.json { render :show, status: :ok, location: @message } else format.html { render :edit } format.json { render json: @message.errors, status: :unprocessable_entity } end end end

# DELETE /messages/1 # DELETE /messages/1.json def destroy @message.destroy respond_to do |format| format.html { redirect_to messages_url, notice: 'Message was successfully destroyed.' } format.json { head :no_content } end end

private # Use callbacks to share common setup or constraints between actions. def set_message @message = Message.find(params[:id]) end

# Never trust parameters from the scary internet, only allow the white list through.
def message_params
  params.require(:message).permit(:name, :email, :company, :phone, :subject, :body)
end

end

development.rb:

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 on
  # every request. 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 and disable caching.
  config.consider_all_requests_local       = true
  config.action_controller.perform_caching = false

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

  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
    address: "smpt.gmail.com",
    port: "587",
    domain: "gmail.com",
    authentication: "plain",
    enable_starttls_auto: true,
    user_name: 'your_email_username',
    password: 'your_email_password'
  }

  CONTACT_RECIPIENT = 'yourname@yourdomain.com'
  # Print deprecation notices to the Rails logger.
  config.active_support.deprecation = :log

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

  # Debug mode disables concatenation and preprocessing of assets.
  # This option may cause significant delays in view rendering with a large
  # number of complex assets.
  config.assets.debug = true

  # Adds additional error checking when serving assets at runtime.
  # Checks for improperly declared sprockets dependencies.
  # Raises helpful error messages.
  config.assets.raise_runtime_errors = true

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

  # Required for Devise gem
  config.action_mailer.default_url_options = { :host => 'localhost:3000' }

end

class ContactMailer < ActionMailer::Base
  default from: "from@example.com"

  # Subject can be set in your I18n file at config/locales/en.yml
  # with the following lookup:
  #
  #   en.contact_mailer.message.subject
  #

    def message(message)
    subject    message.subject
    body       :message => message
    recipients CONTACT_RECIPIENT
    from       message.email
    sent_on    Time.now
    end

  end
end

message.html.erb

Email from your web site

From: <%= @message.name %>

Company: <%= @message.company %>

Phone: <%= @message.phone %>

Message: <%= @message.body %>

messages/_form.html.erb

<%= semantic_form_for @message do |f| %>
  <%= f.inputs do %>
    <%= f.input :name %>
    <%= f.input :company %>
    <%= f.input :phone %>
    <%= f.input :email %>
    <%= f.input :subject %>
    <%= f.input :body %>
  <% end %>

  <%= f.actions do %>
    <%= f.action :submit, :as => :input %>
  <% end %>
<% end %>

the relevant part of the db schema file

 create_table "messages", force: true do |t|
    t.string   "name"
    t.string   "company"
    t.string   "phone"
    t.string   "email"
    t.string   "subject"
    t.text     "body"
    t.datetime "created_at"
    t.datetime "updated_at"
  end
4
  • Did you put your ContactMailer class inside of development.rb? Perhaps taking a look at ActionMailer basics will help as well.
    – Justin
    Sep 15, 2014 at 15:39
  • Is subject a column on the messages table? you can double check your db/schema.rb to verify that it is listed under the messages table. Also, what Justin said.. your ContactMailer should be in app/mailers, not in development.rb Sep 15, 2014 at 15:43
  • i believe the subject column is there-i have edited the question accordingly!
    – Lilp
    Sep 15, 2014 at 15:48
  • when you say put contact mailer inside of development.rb file could you expand a bit. I have already exhausted all the docs i can find including that before i asked on here!
    – Lilp
    Sep 15, 2014 at 15:49

1 Answer 1

1
def deliver_message(message)
  subject    message.subject
  body       :message => message
  recipients CONTACT_RECIPIENT
  from       message.email
  sent_on    Time.now
end

should be

def deliver_message(message)
  @message = message
  mail(subject: message.subject, body: message.body, recipients: CONTACT_RECIPIENT, from: message.email, sent_on: Time.now)
 end
3
  • SyntaxError (/Users/em/new/app/mailers/contact_mailer.rb:26: syntax error, unexpected ':', expecting keyword_end subject: message.subject ^ /Users/em/new/app/mailers/contact_mailer.rb:27: syntax error, unexpected ':', expecting keyword_end body: :message => message ^ /Users/em/new/app/mailers/contact_mailer.rb:28: syntax error, unexpected ':', expecting keyword_end recipients: CONTACT_RECIPIENT ^ /Users/em/new/app/mailers/contact_mailer.rb:29: syntax error, unexpected ':', expecting keyword_end from: message.email ^
    – Lilp
    Sep 15, 2014 at 16:19
  • /Users/em/new/app/mailers/contact_mailer.rb:30: syntax error, unexpected ':', expecting keyword_end sent_on: Time.now ^ /Users/em/new/app/mailers/contact_mailer.rb:34: syntax error, unexpected keyword_end, expecting end-of-input):
    – Lilp
    Sep 15, 2014 at 16:20
  • /Users/em/new/app/mailers/contact_mailer.rb:26: syntax error, unexpected ':', expecting keyword_end subject: message.subject, ^ /Users/em/new/app/mailers/contact_mailer.rb:27: syntax error, unexpected tLABEL, expecting '=' body: message.body, ^ /Users/em/new/app/mailers/contact_mailer.rb:28: syntax error, unexpected tLABEL, expecting '=' recipients: CONTACT_RECIPIENT, ^ /Users/em/new/app/mailers/contact_mailer.rb:28: dynamic constant assignment /Users/em/new/app/mailers/contact_mailer.rb:30: syntax error, unexpected tLABEL, expecting '=' sent_on: Time.now ^
    – Lilp
    Sep 15, 2014 at 17:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.