0

I'm extremely new, and going through the basic ruby rails tutorial except tweaking it just to add some extra fields and renaming what it calls articles at contacts.

Problem is, everything is fine, except all my data is being added in as NIL. Here is my controller:

class ContactsController < ApplicationController
   def index
@contacts = Contact.all
   end
def show
  @contacts = Contact.find(params[:id])
end

def new
 end

def create
@contacts = Contact.new(contact_params)

@contacts.save
redirect_to @contacts
 end
end

private
   def contact_params
     params.require(:contacts).permit(:first_name, :last_name, :phone_number,     :notes)
    end

my migrate file:

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

      t.timestamps null: false
    end
  end
 end

And I'm looking in my terminal and I see this:

 Started POST "/contacts" for 127.0.0.1 at 2015-07-16 14:38:54 -0700
Processing by ContactsController#create as HTML
  Parameters: {"utf8"=>"✓",     "authenticity_token"=>"5l/werImOvbiPAq4rGB6oj8TILHKVVy96GGgRntzP2UwZxj6cSmIDsHzs     RQZXbqHJATp60QMuRg7HWlLY5hf/w==", "contacts"=>{"First_name"=>"Hello",      "Last_name"=>"AAGIN", "Phone_Number"=>"1231313", "Notes"=>"smsda"},     "commit"=>"Save Contacts"}
 Unpermitted parameters: First_name, Last_name, Phone_Number, Notes

I think they're might be something about the 'unpermitted parameters' part, because there seems to be a difference in caps there.

If you see anything wrong, please let me know! Thank you. edit = here's my view:

<h1>New Contact</h1>

<%= form_for :contacts, url: contacts_path do |f| %>
  <p>
    <%= f.label :First_name %><br>
    <%= f.text_field :First_name %>
  </p>
  <p>
    <%= f.label :Last_name  %><br>
    <%= f.text_field :Last_name %>
  </p>
  <p>
     <%= f.label :Phone_Number %><br>
    <%= f.text_field :Phone_Number %>
  </p>
  <p>
    <%= f.label :Notes %><br>
    <%= f.text_area :Notes %>
  </p>

  <p>
  <%= f.submit %>
  </p>
<% end %>

<%= link_to 'Back', contacts_path %>
echa
  • 1
  • 2
  • Hello echa, welcome to StackOverflow. We'll need to see your view as well. :) – BroiSatse Jul 16 '15 at 22:09
  • Thanks everyone for your help, the problem was actually in my view, some naming was wrong. Thanks for pointing out my contacts naming, for sure I need to fix that! : ) – echa Jul 16 '15 at 23:38

2 Answers2

0

You are not consistent with contacts and contact. Change your line to.

 params.require(:contact).permit(:first_name, :last_name, :phone_number,     :notes)

Name your variables according to Rails conventions co @contacts should be changed to @contact.

adamliesko
  • 1,887
  • 1
  • 14
  • 21
-1

You are not instantiating your new object in the new action, so the form is taking a nil object and not filling in with the new data from the form. Try something like

def new
    @contact = Contact.new
end

and also, you are using @contacts instead of @contact in the create and show actions, for the show is just a naming thing, semantics.. but for the create you should use the @contact object that you instantiated in the new action.

Last the syntax for the params is params.require(:contact) no contacts, always read your code in rails and check if the semantics makes sense, it would make you life easy

Alexis
  • 4,836
  • 2
  • 21
  • 27