0

I have some issues with a form that is not posting and there is nothing that is shown in the logs. I am not sure if the problem comes from activemodel, file name conventions, or or routing. No messages are sent.

My controller is pretty basic it's in a file called *promessages_controller.rb*

# coding: utf-8
class PromessagesController < ApplicationController
   def new
      @promessage = Promessage.new
  end
  def create
    @promessage = Promessage.new(params[:promessage])
    if @promessage.valid?
      PromessageMailer.contact_us(@promessage).deliver
      redirect_to(root_path, :notice => "Sent.")
    else
        redirect_to(root_path, :notice => "Error")
    end
  end
end

My mailer is in a file called *promessage_mailer.rb*

class PromessageMailer < ActionMailer::Base
  def contact_us(message)
    @message = message
    mail(:to => 'xxx@gmail.com', :subject => "test", :from => "info@mydomain.com")
  end
end

My activemodel model is in a file called promessage.rb and contains the following:

class Promessage

  include ActiveModel::Validations
  include ActiveModel::Conversion
  extend ActiveModel::Naming

  attr_accessor  :email, :name, :body

  validates :name, :email, :body, :presence => true
  validates :email, :format => { :with => %r{.+@.+\..+} }, :allow_blank => true

  def initialize(attributes = {})
    attributes.each do |name, value|
      send("#{name}=", value)
    end
  end
  def persisted?
    false
  end
end

My form is in new.html.erb and is under views/promessages

<%= simple_form_for @promessage do |f| %>
   <% @promessage.errors.full_messages.each do |msg| %>
      <p><%= msg %></p>
   <% end %>

  <%= f.input :email, label: 'email' %>
  <%= f.input :body, label: 'text', as: :text, :input_html => { :cols => 50, :rows => 5 } %>
  <%= f.submit "Send", :class => 'btn btn-primary' %>
<% end %>

I have my text and html templates in views/promessage_mailer

and in my routes I added:

resources :promessages, only: [:new, :create]

Everytime I submit the form, it shows the error in the controller and doesn't even validate the form. What am I missing?

Ayrad
  • 3,996
  • 8
  • 45
  • 86
  • what error is it showing? – dax Aug 29 '13 at 20:39
  • Can you include the error message and any other pertinent information in your log. – Rob Di Marco Aug 29 '13 at 20:39
  • unfortunately no error is shown in the logs. only the message specified the controller saying "Error". ( redirect_to(root_path, :notice => "Error")) – Ayrad Aug 29 '13 at 21:52
  • in my development environment I do have config.action_mailer.raise_delivery_errors = true – Ayrad Aug 29 '13 at 21:53
  • @Ayrad apparently this is returning false `if @promessage.valid?`. I'd first print out the errors for that object: `logger.debug @promessage.errors`. – Damien Roche Aug 29 '13 at 22:09
  • So here is what I get in the log" Started POST "/promessages" for 127.0.0.1 at 2013-08-30 00:21:48 +0200 Processing by PromessagesController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"DKFAaeDVCQbPZU/jlSQDUngb7c5ZdqPkJCQhs=", "promessage"=>{""email"=>"", "body"=>""}, "commit"=>"Send"} Redirected to http://0.0.0.0:3000/ # Completed 302 Found in 5ms (ActiveRecord: 0.0ms) – Ayrad Aug 29 '13 at 22:23
  • 1
    @Ayrad can you change `logger.debug @promessage.errors` to `logger.debug @promessage.errors.full_messages` and post the trace again? – Bigxiang Aug 30 '13 at 02:29
  • interesting it was actually validation issues that were blocking the sending. My usual notice bar on top wasn't displaying validation errors and wasn't blocking the form from POSTing. Now I get the message "Sent". I'll need to debug why the form wasn't being blocked from posting even with validation errors. Thank you for showing me how to log controller errors and pointing me to the right direction! – Ayrad Aug 30 '13 at 07:08

0 Answers0