Solved

Contact page returning JSON

Posted on 2009-05-10
32
459 Views
Last Modified: 2013-11-13
Hey,

I have my contact page here: http://bestqualitypools.info/contact - when I fill out the form and hit submit I get:

{"success": true}

And if you submit it blank:

{"success": false, "errors": ["State can't be empty", "Message can't be empty", "Product can't be empty", "Email can't be empty", "Email should be like xxx@yyy.zzz", "First name can't be empty", "Last name can't be empty"]}

I am not sure why it is not submitting the form, my controller/model is here:
CONTACT_REQUEST.RB
 
 

class ContactRequest

  include Validatable
 

  attr_accessor :email, :first_name, :last_name, :message, :state, :product
 
 

  validates_presence_of :email, :first_name, :last_name, :message, :state, :product

  validates_format_of    :email,

                         :with => %r{\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z}i, :message => "should be like xxx@yyy.zzz"
 
 

end
 
 

INDEX_CONTROLLER.RB
 

class IndexController < ApplicationController

  def index

    

  end

  

  def pools

    

  end  

  

  def grills
 

  end

  

  def spa

    

  end

  

  def equipment

    

  end

  

  def patio

    

  end

  

  def contact

          if request.post?

            output = ""

            

            @contact = ContactRequest.new

            

            @contact.email = params[:contact]['email']

            @contact.product = params[:contact]['product']

            @contact.first_name = params[:contact]['first_name']

            @contact.last_name = params[:contact]['last_name']

            @contact.state = params[:contact]['state']

            @contact.message = params[:contact]['message']
 

            if @contact.valid?

              

              Notifications.deliver_contact_request(params[:contact])

              render :text => {:success => true}.to_json

              return

            end

            

            render :text => {:success => false, :errors => @contact.errors.full_messages}.to_json

            

            return

          end

  end

  

  def sales

    @promo_image = PromoImage.find(:first)

    

    if @promo_image.nil?

      @partial = "no_sales"

    else

      @partial = "current_sales"

    end

  end

end
 
 

Any ideas on what I can do to get that to work? Not sure why it is doing that.

Open in new window

0
Comment
  • 21
  • 11
32 Comments
 
LVL 12

Expert Comment

by:cminear
ID: 24355095
In the ContactRequest model object, you have the 'validates_presence_of' set.  If you compare the fields included in this line with the error messages, you will see there is one error message for each and every one of those fields.  The 'validates_presence_of' is a model callback which makes sure the model is not saved to the database without those fields having a value.  When this validation fails, the model object is not saved, and an Error object is added to the model object, which includes one or more error messages.

You also have an extra error message, which corresponds to the 'validates_format_of'.  Quite simply, an empty string does not match the format you've specified for an email address.

Not knowing exactly what you're trying to accomplish, you typically want the same form redisplayed to the user, along with the error messages.  The way to display the errors for the model object is to remove the 'render :text (:success => false, ..." line from the controller and add
   <%= error_messages_for @contact %>
(for your case) at the top of the form view template (app/views/index/contact.rhtml, or something like this).  If you do this, the fields which caused the errors should also be bordered by a red outline, to help call out the problem fields.
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 24361192

You also have an extra error message, which corresponds to the 'validates_format_of'.  Quite simply, an empty string does not match the format you've specified for an email address.

^^ For that do you mean this:

  validates_presence_of :email, :first_name, :last_name, :message, :state, :product
  validates_format_of    :email,
                         :with => %r{\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z}i, :message => "should be like xxx@yyy.zzz"

To

  validates_presence_of :first_name, :last_name, :message, :state, :product
  validates_format_of    :email,
                         :with => %r{\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z}i, :message => "should be like xxx@yyy.zzz"

Then:

<%= error_messages_for @contact %>

At the top of my view for contact/

Also

            render :text => {:success => false, :errors => @contact.errors.full_messages}.to_json

To:

            render :text => {:errors => @contact.errors.full_messages}.to_json
0
 
LVL 12

Expert Comment

by:cminear
ID: 24364818
Instead of rendering text, I would allow the default "contact" HTML rendering to occur.  I would think you'd want the form redisplayed to the user who submitted it.  If you just remove the 'render :text => {:success => false, ...}' line from the controller, then invalid contact cases will cause the HTML form to be redisplayed, so the user could potentially correct their errors; this is just like if the user requests the form initially.

Another hint: you can pass in 'params[:contact]' to the 'new' method, so that you don't have to individually set each object field.  Similarly, you should be able to also just send the '@contact' variable to the 'Notifications.deliver_contact_request' action (unless that method is specifically looking for a Hash --- but if it is, that should probably be changed).  Also note that with @contact set and the error case causing the form to be redisplayed, the previously used values would populate the form (if present).

Finally, you probably want the contact saved, so instead of 'valid?', use 'save'.  Since validation is checked before the object is actually saved to the database, the error case is the same; and in cases where the validation is good, the contact will be saved to the database and then notification is sent out.

Of course, I'm making assumptions on what you are trying to accomplish.  Let me know what I assumed wrong.
def contact

  if request.post?

    output = ""

            

    @contact = ContactRequest.new(params[:contact])

            

    if @contact.save

              

      Notifications.deliver_contact_request(@contact)

      render :text => {:success => true}.to_json

      return

    end

  end

end

Open in new window

0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 24366077
I took this project over from another developer. A client came to me and send me the site. I had to go through and reinstall gems/plugins.

Not sure if that helps you. I would have thought it would just work.

I see the form is using:

<script type="text/javascript" src="/global/js/jquery-1.2.1.min.js"></script>
<script type="text/javascript" src="/global/js/jquery.form.js"></script>
<script type="text/javascript" src="/global/js/form.js"></script>

At the top of contact.html.erb. So the form is being submitted via ajax? Let me try that code above.
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 24366121
Using:

def contact
  if request.post?
    output = ""
           
    @contact = ContactRequest.new(params[:contact])
           
    if @contact.save
             
      Notifications.deliver_contact_request(@contact)
      render :text => {:success => true}.to_json
      return
    end
  end
end

I now get the error page, let me check the logs.
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 24366133
Does this help? Let me know if I can do anything else.

Thanks,

Ryan
Processing IndexController#contact (for 74.69.229.154 at 2009-05-12 09:01:49) [POST]

  Session ID: BAh7CToMY3NyZl9pZCIlZmUyNWQ0ZmRlZWYwZWMxMDI4N2I4YjJkMDQ0OTZk

NGM6DnJldHVybl90bzAiCmZsYXNoSUM6J0FjdGlvbkNvbnRyb2xsZXI6OkZs

YXNoOjpGbGFzaEhhc2h7AAY6CkB1c2VkewA6DHVzZXJfaWRpBw==--b08b102410115c5cd630ecfca8a39ab772325b2b

  Parameters: {"form_id"=>"19846", "submit_button"=>"Submit", "contact"=>{"message"=>"asdfadsf", "product"=>"Pool Vacuum", "first_name"=>"asdf", "last_name"=>"asdf", "state"=>"CA", "email"=>"asdfasd"}, "authenticity_token"=>"94cf7b2051cd635c75ac85e5bbc614a432393e3a", "action"=>"contact", "controller"=>"index"}
 
 

ArgumentError (wrong number of arguments (1 for 0)):

    /app/controllers/index_controller.rb:30:in `initialize'

    /app/controllers/index_controller.rb:30:in `new'

    /app/controllers/index_controller.rb:30:in `contact'

    /vendor/rails/actionpack/lib/action_controller/base.rb:1166:in `send'

    /vendor/rails/actionpack/lib/action_controller/base.rb:1166:in `perform_action_without_filters'

    /vendor/rails/actionpack/lib/action_controller/filters.rb:579:in `call_filters'

    /vendor/rails/actionpack/lib/action_controller/filters.rb:572:in `perform_action_without_benchmark'

    /vendor/rails/actionpack/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue'

    /usr/lib/ruby/1.8/benchmark.rb:293:in `measure'

    /vendor/rails/actionpack/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue'

    /vendor/rails/actionpack/lib/action_controller/rescue.rb:201:in `perform_action_without_caching'

    /vendor/rails/actionpack/lib/action_controller/caching/sql_cache.rb:13:in `perform_action'

    /vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb:33:in `cache'

    /vendor/rails/activerecord/lib/active_record/query_cache.rb:8:in `cache'

    /vendor/rails/actionpack/lib/action_controller/caching/sql_cache.rb:12:in `perform_action'

    /vendor/rails/actionpack/lib/action_controller/base.rb:529:in `send'

    /vendor/rails/actionpack/lib/action_controller/base.rb:529:in `process_without_filters'

    /vendor/rails/actionpack/lib/action_controller/filters.rb:568:in `process_without_session_management_support'

    /vendor/rails/actionpack/lib/action_controller/session_management.rb:130:in `process'

    /vendor/rails/actionpack/lib/action_controller/base.rb:389:in `process'

    /vendor/rails/actionpack/lib/action_controller/dispatcher.rb:149:in `handle_request'

    /vendor/rails/actionpack/lib/action_controller/dispatcher.rb:107:in `dispatch'

    /vendor/rails/actionpack/lib/action_controller/dispatcher.rb:104:in `synchronize'

    /vendor/rails/actionpack/lib/action_controller/dispatcher.rb:104:in `dispatch'

    /vendor/rails/actionpack/lib/action_controller/dispatcher.rb:120:in `dispatch_cgi'

    /vendor/rails/actionpack/lib/action_controller/dispatcher.rb:35:in `dispatch'

    /home/66747/data/rubygems/gems/gems/mongrel-1.1.5-x86-linux/bin/../lib/mongrel/rails.rb:76:in `process'

    /home/66747/data/rubygems/gems/gems/mongrel-1.1.5-x86-linux/bin/../lib/mongrel/rails.rb:74:in `synchronize'

    /home/66747/data/rubygems/gems/gems/mongrel-1.1.5-x86-linux/bin/../lib/mongrel/rails.rb:74:in `process'

    /home/66747/data/rubygems/gems/gems/mongrel-1.1.5-x86-linux/bin/../lib/mongrel.rb:159:in `process_client'

    /home/66747/data/rubygems/gems/gems/mongrel-1.1.5-x86-linux/bin/../lib/mongrel.rb:158:in `each'

    /home/66747/data/rubygems/gems/gems/mongrel-1.1.5-x86-linux/bin/../lib/mongrel.rb:158:in `process_client'

    /home/66747/data/rubygems/gems/gems/mongrel-1.1.5-x86-linux/bin/../lib/mongrel.rb:285:in `run'

    /home/66747/data/rubygems/gems/gems/mongrel-1.1.5-x86-linux/bin/../lib/mongrel.rb:285:in `initialize'

    /home/66747/data/rubygems/gems/gems/mongrel-1.1.5-x86-linux/bin/../lib/mongrel.rb:285:in `new'

    /home/66747/data/rubygems/gems/gems/mongrel-1.1.5-x86-linux/bin/../lib/mongrel.rb:285:in `run'

    /home/66747/data/rubygems/gems/gems/mongrel-1.1.5-x86-linux/bin/../lib/mongrel.rb:268:in `initialize'

    /home/66747/data/rubygems/gems/gems/mongrel-1.1.5-x86-linux/bin/../lib/mongrel.rb:268:in `new'

    /home/66747/data/rubygems/gems/gems/mongrel-1.1.5-x86-linux/bin/../lib/mongrel.rb:268:in `run'

    /home/66747/data/rubygems/gems/gems/mongrel-1.1.5-x86-linux/bin/../lib/mongrel/configurator.rb:282:in `run'

    /home/66747/data/rubygems/gems/gems/mongrel-1.1.5-x86-linux/bin/../lib/mongrel/configurator.rb:281:in `each'

    /home/66747/data/rubygems/gems/gems/mongrel-1.1.5-x86-linux/bin/../lib/mongrel/configurator.rb:281:in `run'

    /home/66747/data/rubygems/gems/gems/mongrel-1.1.5-x86-linux/bin/mongrel_rails:128:in `run'

    /home/66747/data/rubygems/gems/gems/mongrel-1.1.5-x86-linux/bin/../lib/mongrel/command.rb:212:in `run'

    /home/66747/data/rubygems/gems/gems/mongrel-1.1.5-x86-linux/bin/mongrel_rails:281
 

Rendering /home/66747/containers/rails/bestqualitypools/public/500.html (500 Internal Server Error)

Open in new window

0
 
LVL 12

Expert Comment

by:cminear
ID: 24366905
OK, I think I understand what is going on, although I don't understand what your predecessor was trying, or why.  The key is the class definition for ContactRequest.  I had assumed that this was inheriting from ActiveRecord::Base, as all Rails model objects do.  But I now see that it isn't, which means that ContactRequest is inheriting from Class.  This means that since you do not have a 'new' or 'initialize' method defined, it doesn't know how to cope with the arguments passed to it.  Thus it is choking on exactly what I suggested.

Where is the contact_request.rb file located?  Is it in 'app/models' or someplace else?  And assuming you have a database for this app, is there a 'contact_requests' table?  I'm starting to believe that all this is trying to do is forward the contact info to some e-mail address, but _not_ save it to a database (unless that is buried in the "Notifications.deliver_contact_request" code).

For now, you should probably revert the code in 'def contact' back to what you had, except for removing the 'render' I suggested removing before.

If there is a 'contact_requests' table in the database, you should investigate changing the class definition for ContactRequest, and then employing the changes I suggested before, unless you can figure out why your predecessor avoided using ActiveRecord natively.

Is there a 'contact.text.erb' file, too?  If so, that will be used with the successful 'render' call.
# your definition for ContactRequest

class ContactRequest
 

# what I'd expected

class ContactRequest < ActiveRecord::Base

Open in new window

0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 24376617
contact_request.rb is located in: models/

I do not see contact_requests as a table in my database.

Sorry, what do you mean by?

you should investigate changing the class definition for ContactRequest, and then employing the changes I suggested before, unless you can figure out why your predecessor avoided using ActiveRecord natively.

Still pretty new with rails, should I migrate and rerun?
0
 
LVL 12

Expert Comment

by:cminear
ID: 24376830
Because ContactRequest does not inherit from ActiveRecord::Base, it is not a true Rails model object; this is further confirmed by the fact that there isn't a table in the database for these contact requests.  

By "investigate", partly I meant looking at other code to see what it does, in relation to this.  However, part of it is also talking to the client about what they really want.  If all they want is an e-mail with the contact information, and they don't care about having it stored away for them, then leaving the current implementation is fine (with a few tweaks).  However, if they would like the contacts saved to a database, then you'd want to change the class definition like I did in my immediately prior comment, and use all the changes in my comment before that.  Of course, you would also have extra work to add interfaces to that contact information, and adding authentication to protect it, and so on.

Also, you may want to talk to the client about what they want the customer to see after a successful contact submission.  I'm thinking the prior developer got the contact e-mail working, so the client was happy and never checked or asked about the user experience.  Or it worked in the developer environment by beyond e-mail it didn't work as expected otherwise.  (Just guessing on my part.)
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 24376947
I was talking to the client and tried getting a hold of the old developer who made this site, i am just doing updates.

If all the files are there before hand and i uploaded to my MediaTemple server, it should run? But apparently not, haha - re run to create the databases?


He just wants the contact info emailed to him, and saying message sent, very basic.

Thank you for your time on this,

Ryan
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 24600868
Hey,

Sorry for the delay, i have been away. i have talked to the client and they want the same implementation that was on the site before. Which is just send an email to the client and with the details from the form, no database and a simple success your mail has been sent, etc.

I have been doing more and more research and can't figure out why it is doing this, any thoughts?

I get this on form submit:
Processing IndexController#contact (for 74.69.229.154 at 2009-06-11 04:29:13) [POST]

  Session ID: BAh7BzoMY3NyZl9pZCIlYTY0YTYyYjAzOTQwMDY4OWU4YTQ3OWY5N2NmN2Q4

ZmQiCmZsYXNoSUM6J0FjdGlvbkNvbnRyb2xsZXI6OkZsYXNoOjpGbGFzaEhh

c2h7AAY6CkB1c2VkewA=--35dee5555860ebaf0d3b1b62e932562ae5ff449b

  Parameters: {"form_id"=>"19846", "submit_button"=>"Submit", "contact"=>{"message"=>"dfh", "product"=>"Inground Pool", "first_name"=>"ryan", "last_name"=>"coughlin", "state"=>"ME", "email"=>"glanceatx@gmail.comas"}, "authenticity_token"=>"5ff730feaa32a83dfaab0b95f0a192b534732d4d", "action"=>"contact", "controller"=>"index"}
 
 

ArgumentError (wrong number of arguments (1 for 0)):

    /app/controllers/index_controller.rb:30:in `initialize'

    /app/controllers/index_controller.rb:30:in `new'

    /app/controllers/index_controller.rb:30:in `contact'

    /vendor/rails/actionpack/lib/action_controller/base.rb:1166:in `send'

    /vendor/rails/actionpack/lib/action_controller/base.rb:1166:in `perform_action_without_filters'

    /vendor/rails/actionpack/lib/action_controller/filters.rb:579:in `call_filters'

    /vendor/rails/actionpack/lib/action_controller/filters.rb:572:in `perform_action_without_benchmark'

    /vendor/rails/actionpack/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue'

    /usr/lib/ruby/1.8/benchmark.rb:293:in `measure'

    /vendor/rails/actionpack/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue'

    /vendor/rails/actionpack/lib/action_controller/rescue.rb:201:in `perform_action_without_caching'

    /vendor/rails/actionpack/lib/action_controller/caching/sql_cache.rb:13:in `perform_action'

    /vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb:33:in `cache'

    /vendor/rails/activerecord/lib/active_record/query_cache.rb:8:in `cache'

    /vendor/rails/actionpack/lib/action_controller/caching/sql_cache.rb:12:in `perform_action'

    /vendor/rails/actionpack/lib/action_controller/base.rb:529:in `send'

    /vendor/rails/actionpack/lib/action_controller/base.rb:529:in `process_without_filters'

    /vendor/rails/actionpack/lib/action_controller/filters.rb:568:in `process_without_session_management_support'

    /vendor/rails/actionpack/lib/action_controller/session_management.rb:130:in `process'

    /vendor/rails/actionpack/lib/action_controller/base.rb:389:in `process'

    /vendor/rails/actionpack/lib/action_controller/dispatcher.rb:149:in `handle_request'

    /vendor/rails/actionpack/lib/action_controller/dispatcher.rb:107:in `dispatch'

    /vendor/rails/actionpack/lib/action_controller/dispatcher.rb:104:in `synchronize'

    /vendor/rails/actionpack/lib/action_controller/dispatcher.rb:104:in `dispatch'

    /vendor/rails/actionpack/lib/action_controller/dispatcher.rb:120:in `dispatch_cgi'

    /vendor/rails/actionpack/lib/action_controller/dispatcher.rb:35:in `dispatch'

    /home/66747/data/rubygems/gems/gems/mongrel-1.1.5-x86-linux/bin/../lib/mongrel/rails.rb:76:in `process'

    /home/66747/data/rubygems/gems/gems/mongrel-1.1.5-x86-linux/bin/../lib/mongrel/rails.rb:74:in `synchronize'

    /home/66747/data/rubygems/gems/gems/mongrel-1.1.5-x86-linux/bin/../lib/mongrel/rails.rb:74:in `process'

    /home/66747/data/rubygems/gems/gems/mongrel-1.1.5-x86-linux/bin/../lib/mongrel.rb:159:in `process_client'

    /home/66747/data/rubygems/gems/gems/mongrel-1.1.5-x86-linux/bin/../lib/mongrel.rb:158:in `each'

    /home/66747/data/rubygems/gems/gems/mongrel-1.1.5-x86-linux/bin/../lib/mongrel.rb:158:in `process_client'

    /home/66747/data/rubygems/gems/gems/mongrel-1.1.5-x86-linux/bin/../lib/mongrel.rb:285:in `run'

    /home/66747/data/rubygems/gems/gems/mongrel-1.1.5-x86-linux/bin/../lib/mongrel.rb:285:in `initialize'

    /home/66747/data/rubygems/gems/gems/mongrel-1.1.5-x86-linux/bin/../lib/mongrel.rb:285:in `new'

    /home/66747/data/rubygems/gems/gems/mongrel-1.1.5-x86-linux/bin/../lib/mongrel.rb:285:in `run'

    /home/66747/data/rubygems/gems/gems/mongrel-1.1.5-x86-linux/bin/../lib/mongrel.rb:268:in `initialize'

    /home/66747/data/rubygems/gems/gems/mongrel-1.1.5-x86-linux/bin/../lib/mongrel.rb:268:in `new'

    /home/66747/data/rubygems/gems/gems/mongrel-1.1.5-x86-linux/bin/../lib/mongrel.rb:268:in `run'

    /home/66747/data/rubygems/gems/gems/mongrel-1.1.5-x86-linux/bin/../lib/mongrel/configurator.rb:282:in `run'

    /home/66747/data/rubygems/gems/gems/mongrel-1.1.5-x86-linux/bin/../lib/mongrel/configurator.rb:281:in `each'

    /home/66747/data/rubygems/gems/gems/mongrel-1.1.5-x86-linux/bin/../lib/mongrel/configurator.rb:281:in `run'

    /home/66747/data/rubygems/gems/gems/mongrel-1.1.5-x86-linux/bin/mongrel_rails:128:in `run'

    /home/66747/data/rubygems/gems/gems/mongrel-1.1.5-x86-linux/bin/../lib/mongrel/command.rb:212:in `run'

    /home/66747/data/rubygems/gems/gems/mongrel-1.1.5-x86-linux/bin/mongrel_rails:281
 

Rendering /home/66747/containers/rails/bestqualitypools/public/500.html (500 Internal Server Error)

Open in new window

0
 
LVL 12

Expert Comment

by:cminear
ID: 24603274
Did you go back to your original ContactRequest class definition?  Everything I suggested assumed that it inherited from ActiveRecord::Base.  Since it doesn't, and you do not define a 'new' method, it would give the error you see if you attempt "ContactRequest.new(params[:contact])" as I originally suggested.
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 24624213
Below is my ContactRequest model. Everything seems to be fine. Does anything stick out to you?

Ryan
class ContactRequest

  include Validatable
 

  attr_accessor :email, :first_name, :last_name, :message, :state, :product
 
 

  validates_presence_of :email, :first_name, :last_name, :message, :state, :product

  validates_format_of    :email,

                         :with => %r{\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z}i, :message => "should be like xxx@yyy.zzz"
 
 

end

Open in new window

0
 
LVL 12

Expert Comment

by:cminear
ID: 24628896
OK, since ContactRequest does appear to be the original, then you need to perform the 'new' action without any arguments.  I've attached what I think is the fix to the 'contact' method for your controller.  It is basically your original controller, but it drops the text rendering for the error case, as you previously indicated that the blank request would redisplay correctly with prior suggested changes above.

Boy it's a little depressing that it took so long to just suggest dropping the one line.  (You may want to consider setting an error variable, so that a user would see why they got the form back.  I've included these below, but it's not necessary.)
class IndexController < ApplicationController
 

...

  

  def contact

          if request.post?

            output = ""

            

            @contact = ContactRequest.new

            

            @contact.email = params[:contact]['email']

            @contact.product = params[:contact]['product']

            @contact.first_name = params[:contact]['first_name']

            @contact.last_name = params[:contact]['last_name']

            @contact.state = params[:contact]['state']

            @contact.message = params[:contact]['message']

 

            if @contact.valid?

              

              Notifications.deliver_contact_request(params[:contact])

              render :text => {:success => true}.to_json

              return

            end

           

            # possible extra

            @error = "One or more required fields were not set.  Please correct and resubmit."

          end

  end

  

...
 

end

 
 

# possible addition to contact.html.erb

<% unless @error.nil? %>

<p><b><%= @error %></b></p>

<% end %>

Open in new window

0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 24637883
Hey,

Thank you, we are getting much closer! If you go to: http://bestqualitypools.info/contact and try it out. It shows the JSON now.

"{"success": true}"

 So basically you added:

            @contact = ContactRequest.new

To initiate the new state? Any ideas what to do now?

Thank you so much,

RYan
0
 
LVL 12

Expert Comment

by:cminear
ID: 24638835
Well obviously the JSON response is not working as expected.  But then again, do we have any evidence that it ever worked?  (Had the client ever tried submitting the form themselves and described what they saw with a successful submit?)

At this point, I would probably recommend skipping the JSON.  Instead, just create a "success" page and render that.  The 'success.html.erb' would be in 'app/views/index/', the same as 'contact.html.erb'; it would just be the HTML you want to display letting the customer know that their information was accepted.  (I just made a guess so please review it before putting it in place.)  It assumes there is a shared layout for the webapp that it is just filing in.

If you still want to get the JSON working, the first step may be to change "render :text ..." to "render :json ...".  I'd be a little surprised if that just magically got it working, but if that works, you could probably go back to the original version (with ":text" replaced by ":json" in those two places).  However, if it doesn't, then there is probably some other piece missing.  And if the 'render :html..." version works, why continue chasing this method?
class IndexController < ApplicationController

 

...

  

  def contact

          if request.post?

            output = ""

            

            @contact = ContactRequest.new

            

            @contact.email = params[:contact]['email']

            @contact.product = params[:contact]['product']

            @contact.first_name = params[:contact]['first_name']

            @contact.last_name = params[:contact]['last_name']

            @contact.state = params[:contact]['state']

            @contact.message = params[:contact]['message']

 

            if @contact.valid?

              

              Notifications.deliver_contact_request(params[:contact])

              render :action => 'success'

              return

            end

           

            # possible extra

            @error = "One or more required fields were not set.  Please correct and resubmit."

          end

  end
 
 

# example of 'success.html.erb'

<p>Thank you for submitting your information.  We look forward to assisting 

you with your pool problems.</p>

Open in new window

0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 24648446
Also , I did notice this at the top of the page:

<% content_for :js do -%>
<script type="text/javascript" src="/global/js/jquery-1.2.1.min.js"></script>
<script type="text/javascript" src="/global/js/jquery.form.js"></script>
<script type="text/javascript" src="/global/js/form.js"></script>

So it is trying to submit via AJAX.

In the mean time let me try your version.
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 24648464
But then the call for the jQuery needs to be somewhere correct?
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 24648549
The form does SEEM to submit, so I will see if the owner got my email, check it out: http://bestqualitypools.info/contact

So it seemed to be the JSON?

Ryan
0
 
LVL 12

Expert Comment

by:cminear
ID: 24649384
I did delve into those three Javascript files.  It sort of looks like the former developer was starting to play with JSON, but possibly never completed the work, or you got part of it but not all of it.  The real proof would be if there is an "observe_field" call somewhere in the "contact.html.erb" file.  If there isn't any, then I question whether it ever worked correctly.

Plus, just looking at the HTML for the contact page, the form is submitted without Javascript involved, so a vanilla Rails/HTML response is the best.  
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 24670079
Ahh..I see! big mess being handed a job you didnt do. haha.

I appreciate all of your help. I tried it and it seemed to work, but the client said he didnt get any emails. I am trying to find out where that email would be specified? I checked around and couldn't find anything.

Should I check the logs to see if it went through?

Ryan
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 24670213
Here is an output from the log:
Processing IndexController#contact (for 74.69.229.154 at 2009-06-19 14:20:20) [POST]

  Session ID: BAh7BzoMY3NyZl9pZCIlMDUzZTYzODRlZWYxOWM3NmI3MzQ0M2RiNmM2OGE3

ZTMiCmZsYXNoSUM6J0FjdGlvbkNvbnRyb2xsZXI6OkZsYXNoOjpGbGFzaEhh

c2h7AAY6CkB1c2VkewA=--3ea6dae0d9ceb41e1c4ce76f54d024eb44c5ccfb

  Parameters: {"form_id"=>"19846", "submit_button"=>"Submit", "contact"=>{"message"=>"sdsdfasdfasd", "product"=>"Filtering System", "first_name"=>"ryan", "last_name"=>"coughlin", "state"=>"AR", "email"=>"glanceatx@gmail.com"}, "authenticity_token"=>"c72e12b27136f21900885d16e9aa27cd1d5118bc", "action"=>"contact", "controller"=>"index"}

Sent mail to bestqualitypools@aol.com,villamike@aol.com

Rendering template within layouts/application

Rendering index/success

Completed in 0.09969 (10 reqs/sec) | Rendering: 0.00405 (4%) | DB: 0.00018 (0%) | 200 OK [http://bestqualitypools.info/contact]

Open in new window

0
 
LVL 12

Expert Comment

by:cminear
ID: 24670403
Well, there has to be a 'Notifications' class somewhere.  I'd check either app/models, lib or under vendor.  And the log does show "Sent mail to bestqualitypools@aol.com,villamike@aol.com" (line 6 above), so some code somewhere thinks it sent an email.  

If you can find the Notifications code and it looks OK, you could test that specifically by quickly changing the e-mail addresses to something you'll receive and then start up Rail's console: script/console (or script/console production).  This is very much like a Ruby 'irb' session, but it will load all of your Rails model objects.  (Of course, if Notifications is in a location which script/console _doesn't_ pick up naturally, then the rest will not work.)  Then you should be able to create your own contact hash, and then send it to 'Notifications.deliver_contact_request' and see if you get the e-mail.  At this point, it may be a e-mail configuration issue, either with the hosting provider or the Rails environment.  (The environment points to the previous hosting provider's SMTP server.  Check for ActionMailer settings.)

0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 24670510
I found it, let me take a look:
class Notifications < ActionMailer::Base
 

  def contact_request(contact)

    @subject    = 'Message from a Customer'

    @body       = {

      :sender  => '"'+contact[:first_name]+' '+contact[:last_name]+'" <'+contact[:email]+'>',

      :email => contact[:email],

      :product => contact[:product],

      :state => contact[:state],

      :message => contact[:message]

    }

    #@recipients = 'cavaliere.mike@gmail.com'

    @recipients = 'bestqualitypools@aol.com,villamike@aol.com'

    @from       = 'Bestqualitypools Quote Request'

    @sent_on    = Time.now

    @headers    = {}

  end

end

Open in new window

0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 24670516
I commented out the email contact and made it mine to test.
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 24670536
Below is my latest with my email in there, still no email, checked Spam as well. I know how it can delay sometimes, so ill see. Anything else in the mean time to check? Logs see it as success.
class Notifications < ActionMailer::Base
 

  def contact_request(contact)

    @subject    = 'Message from a Customer'

    @body       = {

      :sender  => '"'+contact[:first_name]+' '+contact[:last_name]+'" <'+contact[:email]+'>',

      :email => contact[:email],

      :product => contact[:product],

      :state => contact[:state],

      :message => contact[:message]

    }

    #@recipients = 'cavaliere.mike@gmail.com'

    #@recipients = 'bestqualitypools@aol.com,villamike@aol.com'

    @recipients = 'glanceatx@gmail.com'

    

    @from       = 'Bestqualitypools Quote Request'

    @sent_on    = Time.now

    @headers    = {}

  end

end

 

Open in new window

0
 
LVL 12

Expert Comment

by:cminear
ID: 24682490
Is there a view file for this e-mail?  It should be 'app/views/notifications/contact_request.erb' (or possibly a different extension than .erb).  Confirm that that's there and that it looks OK.

But I don't think that's the issue.  Take a look at config/environment.rb and config/environments/production.rb. There should be a few lines that configure "ActionMailer::Base.smtp_settings", or maybe '.sendmail_settings'.  (It would be best if they are in the production.rb file, but they should only be in one of them.)  Right now, my guess is that your Rails app is still configured to use the SMTP server of the previous hosting provider, and you need to change one or more settings for your new hosting provider.  (To help debug, you may want to set
'ActionMailer::Base.raise_delivery_errors = true' in the production.rb config file.)
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 24687200
Below is my prodction.rb file. Originally the line:

 config.action_mailer.raise_delivery_errors = true

Was set for true.


"Right now, my guess is that your Rails app is still configured to use the SMTP server of the previous hosting provider, and you need to change one or more settings for your new hosting provider."
^^ Any ideas on how I could change that? I can take a search on google in the mean time.


Thank you

# Settings specified here will take precedence over those in config/environment.rb
 

# The production environment is meant for finished, "live" apps.

# Code is not reloaded between requests

config.cache_classes = true
 

# Use a different logger for distributed setups

# config.logger = SyslogLogger.new
 

# Full error reports are disabled and caching is turned on

config.action_controller.consider_all_requests_local = false

config.action_controller.perform_caching             = true

config.action_view.cache_template_loading            = true
 

# Use a different cache store in production

# config.cache_store = :mem_cache_store
 

# Enable serving of images, stylesheets, and javascripts from an asset server

# config.action_controller.asset_host                  = "http://assets.example.com"
 

# Disable delivery errors, bad email addresses will be ignored

# config.action_mailer.raise_delivery_errors = false

 config.action_mailer.raise_delivery_errors = true

 config.action_mailer.delivery_method = :sendmail

Open in new window

0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 24687220
Something like this:


  ActionMailer::Base.smtp_settings = {

    :address        => 'smtp.yourserver.com', # default: localhost

    :port           => '25',                  # default: 25

    :user_name      => 'user',

    :password       => 'pass',

    :authentication => :plain                 # :plain, :login or :cram_md5

  }

Open in new window

0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 24687231
0
 
LVL 12

Accepted Solution

by:
cminear earned 500 total points
ID: 24692615
The last line of the 'production.rb' config file says that ActionMailer is configured to use Sendmail, meaning it will try to run '/usr/sbin/sendmail' directly to send the e-mail.  Either that is not present, your login does not have permission to run sendmail, or something upstream is preventing it from working.

With a little searching, I found this KnowledgeBase article: http://kb.mediatemple.net/questions/148/(gs)+Sending+Mail+using+Ruby+On+Rails

Translating their code suggestion for Rails 2.0 (I'm assuming you are using that) to your installation, you should add the following code to the production.rb file.  Also comment out the " config.action_mailer.delivery_method = :sendmail" line, or change it to be "... :smtp", so that SMTP is used.

Other than these configuration issues, the SMTP chain may have a problem with the sender not being from the 'bestqualitypools.info" domain.  According to the code, it tries to send the e-mail as if it were sent by the submitter directly.  However, as implied by this page (http://kb.mediatemple.net/questions/58/Sending+Email+via+an+SMTP+Server), AOL and/or GMail and/or MediaTemple themselves may not accept e-mail from MediaTemple's SMTP servers which claim to come from joe_shmoe@othersite.com.  As a test (once you sort out the other configuration items and e-mail still does not seem to be flowing), try submitting a contact request using an address with "@bestqualitypools.info" as the domain.  If that e-mail gets through but another with a different domain does not, then you'll have to change the code so that the e-mail sender uses a 'bestqualitypools.info' address, or sending to a local e-mail account may also work.

In any case, once the config changes recommended are in place, this will become more of an e-mail connectivity issue than a specific Rails issue.
ActionMailer::Base.smtp_settings = {

:address => "mail.bestqualitypools.info",

:port => 25,

:domain => "bestqualitypools.info",

:authentication => :login,

:user_name => "<your-username>",

:password => "<your-password>"

}

Open in new window

0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 24697318
Hey,

Just watend to say thank you, so much for your help. Thanks for walking me through this.

Makes sense now. i changed that info up and created a new MediaTemple email and it worked..

Again...thank you!

RYan
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Article by: narshlob
If you've ever programmed in Ruby and have come across either a proc or a lambda, you might have been wondering what the difference is between the two and when you would use one over the other. This article will try to explain the difference between…
Recently I spent hours debugging an issue in a Rails project where ActiveRecord was causing MySQL errors trying to create a User object of a class at the top level of a Single Table Inheritance model structure.  It turns out `.create` behaves differ…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now