1

I'm using Rails 4, and trying to save data via a form, but the process continually fails. I've done my best to look at others who had similar issues but even copying their code doesn't work for me. The end result after filling out my form is receiving the error stating the contact was not saved. Any ideas? Thanks!

controllers/contacts_controller.rb

class ContactsController < ApplicationController

  def create
    @contact = Contact.new(contact_params)
    if @contact.save
      flash[:success] = "Contact saved!"
    else
      flash[:alert] = "Contact not saved!"
    end
  end

  def destroy
  end

  def new
    @contact = Contact.new
  end

  private

    def contact_params
      params.require(:contact).permit(:first_name, :last_name, :email)
    end
end

views/contacts/new.html.erb

<div class= "panel-body">
  <%= form_for(@contact) do |f| %>

  <div class="form-group">
    <%= f.label :first_name %>
    <%= f.text_field :first_name, autofocus: true, class: "form-control" %>
  </div>

  <div class="form-group">
    <%= f.label :last_name %>
    <%= f.text_field :last_name, autofocus: true, class: "form-control" %>
  </div>        

  <div class="form-group">
    <%= f.label :email %>
    <%= f.email_field :email, autofocus: true, class: "form-control" %>
  </div>

  <div class="form-group">
    <%= f.submit "Add", class: "btn btn-primary" %>
  </div>

  <% end %>
</div>

config/routes.rb

Rails.application.routes.draw do

  resources :contacts, only: [:new, :create, :destroy]

end

models/contact.rb

class Contact < ActiveRecord::Base

    validates :first_name, presence: true
    validates :last_name, presence: true
    validates :email, presence: true    

end

db/migrate

class CreateContacts < ActiveRecord::Migration
  def change
    create_table :contacts do |t|
      t.string :first_name
      t.string :last_name
      t.string :email

      t.timestamps
    end
  end
end
Tamiz Ahmed
  • 175
  • 2
  • 14
  • 1
    Can we see the error message and your migrate for the contacts table please. – Matt Jun 17 '14 at 08:08
  • Since you are getting your alert message i think there would be some validations which are failing. Can you post your contact model? – Mandeep Jun 17 '14 at 08:14
  • there is no error message...contact simply doesn't save and the flash for "contact not saved" appears. added model and migration to description. – Tamiz Ahmed Jun 17 '14 at 08:14
  • can u write a code like P contact_params inside def create and tell me its output from the console – Nidhin S G Jun 17 '14 at 09:48
  • @NidhinSG: unclear exactly what you would like me to do. Take my contact params and define them in the create? – Tamiz Ahmed Jun 17 '14 at 20:55

2 Answers2

1

Since you are getting your alert message i think there would be some validations which are failing.

My thoughts exactly - issues when you call .save can have several causes:

  • Validation errors
  • Your database might be incomplete (need to run rake db:migrate)
  • You may have issues with your data structure

I would do this:

  1. Remove any validations from your Contact model
  2. Make sure you perform rake db:migrate (to migrate your datatable)
  3. Ensure your params hash is populated when you pass it from your view to your controller

--

Errors

When you mention there are no errors, there won't be unless you actually display them on your site:

Referencing this answer, you may wish to do this:

<%= form_for @contact do |f| %>
    <% if @location.errors.any? %>
    <ul>
      <% @location.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
    </ul>
    <% end %>
<% end %>
Community
  • 1
  • 1
Richard Peck
  • 76,116
  • 9
  • 93
  • 147
  • I tried each of the following without success: 1. Removed validations from Contact model 2. Ran rake db:migrate I wasn't sure what you meant by #3 ("Ensure your params hash is populated when you pass it from your view to your controller"). How do I check that? I also implemented the error displaying you suggested, but still no errors displayed on my site. Just the flash message: http://d.pr/i/GNVO – Tamiz Ahmed Jun 17 '14 at 20:52
0

Hi I just encounter this problem.

Read the link provided in this thread and going over few other screen cast regarding layouts and partials file.

Conclusion:

  • rendering the form by partial is not the cause you just need to make sure that the instance variable is instantiated in the controller class (in 'new' action)
  • The cause for me I was wrapping the form_for inside a "" tag therefor causing haywire with the submit button (Submit button will submit the form into url specified in "action" attribute of the form) and due to this wrapping my form doesn't have action attribute.

so when I click the submit button it was just reload / refresh the page.

for other encountering the matter, you have to inspect the generated form tag.

P.S: the informative link is a must to read for rookies in Rails like me, turn out we can also send variable to the partial and not only limited by :provide & yield method.

Rajan
  • 312
  • 3
  • 13