mardi 11 octobre 2016

No confirmation messages after submit

I'm working in a Rails 4.1.0 application in cloud9 IDE. I enter test info in the fields: Name, Email, Comments, in the Contact Us form page, clicked submit and the following info displays in the address bar with no 'Message sent' confirmation:

https://manual-coder-rails/contacts?utf8=%E2%9C%93&contact%5Bname%5D=Nick&contact%5Bemail%5D=victorysupplys%40gmail.com&contact%5Bcomments%5D=Nice+web&commit=Submit

I entered no info in the fields (blank), clicked submit, the blank info is displayed in the address bar, again no 'Error' confirmation message:

https://manual-coder-rails/contacts?utf8=%E2%9C%93&contact%5Bname%5D=&contact%5Bemail%5D=&contact%5Bcomments%5D=&commit=Submit

I enter bundle exec rails c and Contact.all in the console, no entries are listed here and no entries are saved to the database:

Contact Load (2.9ms) SELECT "contacts".* FROM "contacts" => #

I tried to figure out what the problem is in my code, having made several changes, but to no avail. Someone might notice something I do not. Here is my code for perusal and advice:

New.html.erb

        <div class="row">
          <div class="col-md-4 col-md-offset-4">
             <div class="well">
               <%= form_for @contact, :method => :get do |f| %>
             <div class="form-group">
                <%= f.label :name %>
                <%= f.text_field :name, class: 'form-control' %>
             </div>
             <div class="form-group">
                <%= f.label :email %>
                <%= f.email_field :email, class: 'form-control' %>
             </div>
             <div class="form-group">
                <%= f.label :comments %>
                <%= f.text_area :comments, class: 'form-control' %>
             </div>
                <%= f.submit type="Submit", class: "btn btn-default" %>
             <% end %>
            </div>
        </div>
     </div>

Contacts_controller.rb

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

          def index
          end  

          def create
            @contact = Contact.new(contact_params)

              if @contact.save
                name = params[:contact][:name]
                email = params[:contact][:email]
                comments = params[:contact][:comments]
                ContactMailer.contact_email(name, email, comments ).deliver

                flash[:success] = "Message sent."
                redirect_to new_contact_path
              else
                flash[:danger] = "Error occured, message has not been sent."
                redirect_to new_contact_path
              end
          end

          private
            def contact_params
              params.require(:contact).permit(:name, :email, :comments)
            end
          end

routes.rb

          Rails.application.routes.draw do
             resources :contacts, only:[:new, :create], path_names: { new: ""}
             get '/about' => 'pages#about'
             root 'pages#home'

application.html.erb

           <div class="container">
              <% flash.each do |key, value| %>
              <div class="alert alert-<% key %> alert-dismissable">
                <button type="button", class="close", data-dismiss="alert", aria-label="Close"><span aria-hidden="true"></span>&times;></span></button>
                <%= value %>
               </div>
               <% end %>

          <%= yield %>
        </div>
      </body>
   </html>

Aucun commentaire:

Enregistrer un commentaire