0

I'm a beginning Rails developer and am creating my first project on my own. It's an online resume for myself. Currently I'm implementing a "Contact Me" page that will send emails to myself. I use simple_form and mail_form gems for this functionality.

I can successfully send emails to myself in the rails console with:

$ @contact = Contact.new(:name => 'Tyler', :email => 'tyler@colorado.edu', :message => 'Here's my message')
$ @contact.deliver

And I will get the message in my development smtp server.

Where I run into trouble is my "app/views/contacts/new.html.erb" view and app/controllers/contacts_controller.rb. Here's my contacts/new view:

<% provide(:title, 'Contact Me') %>

<div class="center hero-unit">
   <h1> Contact Me! </h1>
   <h2> 
       This can include job inquiries, site questions, or general questions. 
   </h2>
</div>
<div class="center">
  <form role="form">
     <%= simple_form_for @contact do |f| %>
        <%= f.input :name, 
        label: 'Name:', 
        placeholder: 'Name', 
        :required => true, 
        :input_html => {:style=> 'width: 450px'}
     %>

     <%= f.input :email, 
        label: 'Email:', 
        placeholder: 'user@domain.com', 
        :required => true, 
        :input_html => {:style=> 'width: 450px'} 
     %>

     <%= f.input :message, 
        label: 'Message:', 
        :required => false, 
        :as => :text, 
        :input_html => {:style=> 'width: 450px'} 
     %>

    <div class="hidden">
       <%= f.input :nickname, :hint => 'Leave this field blank!' %>
    </div>

    <%= f.button :submit, 'Send Message', :action => :create %>
    <%= link_to "Cancel", '/', class: "btn" %>
<% end %>
</form>
</div>

Here's my contacts_controller.rb

class ContactsController < ApplicationController
   def new
      @contact = Contact.new
   end

   def create
      @contact = Contact.new(params[:contact])
      @contact.request = request

      if @contact.deliver
         flash[:success] = "Thanks for reaching out, #{@contact.name}! I will get back to you back shortly!"
         redirect_to @contact
      else
         render 'new'
      end
   end

end

And just in case it helps, here's my routes.rb file:

ResumeProject::Application.routes.draw do

   resources :contacts, only: [:new, :create]
   match    '/contact_me',  to: 'contacts#new',         via: 'get'
   match    '/contact_me',  to: 'contacts#create',      via: 'post' 

   root     'base_home#home'
   match    '/about_me',    to: 'base_home#about_me',   via: 'get'
   match    '/about_site',  to: 'base_home#about_site', via: 'get'

end

Also, if necessary, here's my "/app/views/contacts/create.html.erb

<% provide(:title, 'Thanks!') %>

<h1> Thanks for reaching out! I will message you back shortly! </h1>

<center>
   <%= link_to "Return to Home", '/', class: "btn" %>
</center>

Rails Debugger immediately after the form is submitted:

--- !ruby/hash:ActionController::Parameters
utf8: ✓
authenticity_token: 9233nIpl/EopBBK2g722ZgMDqpD1mMi+BfUsL4/7z5g=
contact: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
  name: Tyler
  email: tyler@colorado.edu
  message: Heres my message
  nickname: ''
commit: Send Message
controller: contacts
action: new

What happens is I will fill in these three fields (name, email, and message) with expected and valid information, hit the "Send Message" button that submits the data, but I will be redirected to the same page with all forms blank again. I've implemented a debugger into the website thanks to Michael Hartl's Rails Tutorial and here's what it says after submitting valid information into the form. ![Screenshot][1]

Shouldn't this be calling the "create" :action and not the "new" :action? I cannot really figure out why this isn't calling what a URL POST should be doing, and thus calling the "create" controller method and not "new".

Any help would sincerely be appreciated! I've been trying to fix this issue all day now and am getting quite frustrated with it. This is a huge gap in my knowledge of Rails that I would really appreciate understanding!

EDIT: Here's the log from the terminal window ($ rails server) when I render the view. Its a bunch of GET requests:

Started GET "/contact_me" for 127.0.0.1 at 2014-08-21 21:28:26 -0600
Processing by ContactsController#new as HTML
  Rendered contacts/new.html.erb within layouts/application (4.0ms)
  Rendered layouts/_shim.html.erb (0.0ms)
  Rendered layouts/_header.html.erb (0.4ms)
  Rendered layouts/_footer.html.erb (0.0ms)
Completed 200 OK in 14ms (Views: 13.9ms | ActiveRecord: 0.0ms)


Started GET "/assets/application.css?body=1" for 127.0.0.1 at 2014-08-21 21:28:26 -0600
Started GET "/assets/base_home.css?body=1" for 127.0.0.1 at 2014-08-21 21:28:26 -0600
Started GET "/assets/contacts.css?body=1" for 127.0.0.1 at 2014-08-21 21:28:26 -0600
Started GET "/assets/custom.css?body=1" for 127.0.0.1 at 2014-08-21 21:28:26 -0600
Started GET "/assets/jquery.js?body=1" for 127.0.0.1 at 2014-08-21 21:28:26 -0600
Started GET "/assets/jquery_ujs.js?body=1" for 127.0.0.1 at 2014-08-21 21:28:26 -0600
Started GET "/assets/bootstrap-transition.js?body=1" for 127.0.0.1 at 2014-08-21 21:28:26 -0600
Started GET "/assets/bootstrap-affix.js?body=1" for 127.0.0.1 at 2014-08-21 21:28:26 -0600
Started GET "/assets/bootstrap-alert.js?body=1" for 127.0.0.1 at 2014-08-21 21:28:26 -0600
Started GET "/assets/bootstrap-button.js?body=1" for 127.0.0.1 at 2014-08-21 21:28:26 -0600
Started GET "/assets/bootstrap-carousel.js?body=1" for 127.0.0.1 at 2014-08-21 21:28:26 -0600
Started GET "/assets/bootstrap-collapse.js?body=1" for 127.0.0.1 at 2014-08-21 21:28:26 -0600
Started GET "/assets/bootstrap-dropdown.js?body=1" for 127.0.0.1 at 2014-08-21 21:28:26 -0600
Started GET "/assets/bootstrap-modal.js?body=1" for 127.0.0.1 at 2014-08-21 21:28:26 -0600
Started GET "/assets/bootstrap-scrollspy.js?body=1" for 127.0.0.1 at 2014-08-21 21:28:26 -0600
Started GET "/assets/bootstrap-tab.js?body=1" for 127.0.0.1 at 2014-08-21 21:28:26 -0600
Started GET "/assets/bootstrap-tooltip.js?body=1" for 127.0.0.1 at 2014-08-21 21:28:26 -0600
Started GET "/assets/bootstrap-popover.js?body=1" for 127.0.0.1 at 2014-08-21 21:28:26 -0600
Started GET "/assets/bootstrap-typeahead.js?body=1" for 127.0.0.1 at 2014-08-21 21:28:26 -0600
Started GET "/assets/bootstrap.js?body=1" for 127.0.0.1 at 2014-08-21 21:28:26 -0600
Started GET "/assets/turbolinks.js?body=1" for 127.0.0.1 at 2014-08-21 21:28:26 -0600
Started GET "/assets/base_home.js?body=1" for 127.0.0.1 at 2014-08-21 21:28:26 -0600
Started GET "/assets/contacts.js?body=1" for 127.0.0.1 at 2014-08-21 21:28:26 -0600
Started GET "/assets/application.js?body=1" for 127.0.0.1 at 2014-08-21 21:28:26 -0600

Here's the log of when I submit the form:

Started GET "/contact_me?    utf8=%E2%9C%93&authenticity_token=9233nIpl%2FEopBBK2g722ZgMDqpD1mMi%2BBfUsL4%2F7z5g%3D&contact%5Bname%5D=Tyler&contact%5Bemail%5D=tyler%40colorado.edu&contact%5Bmessage%5D=Heres+my+message&contact%5Bnickname%5D=&commit=Send+Message" for 127.0.0.1 at 2014-08-21 21:34:08 -0600
Processing by ContactsController#new as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"9233nIpl/EopBBK2g722ZgMDqpD1mMi+BfUsL4/7z5g=", "contact"=>{"name"=>"Tyler", "email"=>"tyler@colorado.edu", "message"=>"Heres my message", "nickname"=>""}, "commit"=>"Send Message"}
  Rendered contacts/new.html.erb within layouts/application (8.8ms)
  Rendered layouts/_shim.html.erb (0.1ms)
  Rendered layouts/_header.html.erb (1.1ms)
  Rendered layouts/_footer.html.erb (0.1ms)
Completed 200 OK in 25ms (Views: 25.1ms | ActiveRecord: 0.0ms)
Started GET "/assets/application.css?body=1" for 127.0.0.1 at 2014-08-21 21:34:09 -0600
Started GET "/assets/base_home.css?body=1" for 127.0.0.1 at 2014-08-21 21:34:09 -0600
Started GET "/assets/contacts.css?body=1" for 127.0.0.1 at 2014-08-21 21:34:09 -0600
Started GET "/assets/custom.css?body=1" for 127.0.0.1 at 2014-08-21 21:34:09 -0600
Started GET "/assets/jquery.js?body=1" for 127.0.0.1 at 2014-08-21 21:34:09 -0600
Started GET "/assets/jquery_ujs.js?body=1" for 127.0.0.1 at 2014-08-21 21:34:09 -0600
Started GET "/assets/bootstrap-transition.js?body=1" for 127.0.0.1 at 2014-08-21 21:34:09 -0600
Started GET "/assets/bootstrap-affix.js?body=1" for 127.0.0.1 at 2014-08-21 21:34:09 -0600
Started GET "/assets/bootstrap-alert.js?body=1" for 127.0.0.1 at 2014-08-21 21:34:09 -0600
Started GET "/assets/bootstrap-button.js?body=1" for 127.0.0.1 at 2014-08-21 21:34:09 -0600
Started GET "/assets/bootstrap-carousel.js?body=1" for 127.0.0.1 at 2014-08-21 21:34:09 -0600
Started GET "/assets/bootstrap-collapse.js?body=1" for 127.0.0.1 at 2014-08-21 21:34:09 -0600
Started GET "/assets/bootstrap-dropdown.js?body=1" for 127.0.0.1 at 2014-08-21 21:34:09 -0600
Started GET "/assets/bootstrap-modal.js?body=1" for 127.0.0.1 at 2014-08-21 21:34:09 -0600
Started GET "/assets/bootstrap-scrollspy.js?body=1" for 127.0.0.1 at 2014-08-21 21:34:09 -0600
Started GET "/assets/bootstrap-tab.js?body=1" for 127.0.0.1 at 2014-08-21 21:34:09 -0600
Started GET "/assets/bootstrap-tooltip.js?body=1" for 127.0.0.1 at 2014-08-21 21:34:09 -0600
Started GET "/assets/bootstrap-popover.js?body=1" for 127.0.0.1 at 2014-08-21 21:34:09 -0600
Started GET "/assets/bootstrap-typeahead.js?body=1" for 127.0.0.1 at 2014-08-21 21:34:09 -0600
Started GET "/assets/bootstrap.js?body=1" for 127.0.0.1 at 2014-08-21 21:34:09 -0600
Started GET "/assets/turbolinks.js?body=1" for 127.0.0.1 at 2014-08-21 21:34:09 -0600
Started GET "/assets/base_home.js?body=1" for 127.0.0.1 at 2014-08-21 21:34:09 -0600
Started GET "/assets/contacts.js?body=1" for 127.0.0.1 at 2014-08-21 21:34:09 -0600
Started GET "/assets/application.js?body=1" for 127.0.0.1 at 2014-08-21 21:34:09 -0600
  • can you gist the logs of what happens when you render the form and then submit the form? – sevenseacat Aug 22 '14 at 03:09
  • also, it should be `redirect_to @contact`, not `redirect_to '@contact'`. You're trying to redirect to the string '@contact', not the actual contact object. – sevenseacat Aug 22 '14 at 03:10
  • First off, thanks for offering your help! I'm very happy that someone has responded to this. When I render the form, I get a big list of GET requests (as expected). I'm adding the full log to the post now! – user3514852 Aug 22 '14 at 03:31

1 Answers1

0

OK, the problem here is actually identical to this one: Ruby on Rails: Data not saving ActiveRecord

And the solution is the same.

You have generated invalid HTML - a form within a form - and so your form is actually submitting via GET (due to the outer form), not via POST.

If you remove the outer <form> element (simple_form_for will create one for you) the problem should go away.

Community
  • 1
  • 1
sevenseacat
  • 24,699
  • 6
  • 63
  • 88
  • IT WORKED! Thank you so much. You have no idea how greatful I am for your help! I've been trying to figure this issue out for more than 6 hours straight earlier today. I really appreciate it, seriously! – user3514852 Aug 22 '14 at 04:34