Tuyen Nguyen
Tuyen Nguyen

Reputation: 4479

Ruby on Rails 4 - Can't update data after adding one extra field to model

I built a RoR application as followed:
(I am at application root while typing the commands )

1- CREATE TICKET TABLE

rails generate scaffold ticket name:string seat_id_seq:string address:text price_paid:decimal email_address:string
rake db:migrate RAILS_ENV=development
rake db:migrate RAILS_ENV=test
rake db:migrate RAILS_ENV=production

2 - The application works and I could add data to database via RoR web interface

3 - I added phone field to the ticket table

rails generate migration AddPhoneToTickets phone:string
rake db:migrate RAILS_ENV=development
rake db:migrate RAILS_ENV=test
rake db:migrate RAILS_ENV=production

4 - I updated following VIEW files to add phone field to the VIEW files

sudo nano app/views/tickets/_form.html.erb
sudo nano app/views/tickets/index.html.erb

5 - The application works, but I can't add phone to existing ticket records or a new record.

Please let me know if I'm missing something here.

Upvotes: 1

Views: 1230

Answers (2)

sites
sites

Reputation: 21795

You should show model for Ticket. And also make sure to read your server log.

But most probably your are failing in adding phone to attr_accessible:

attr_accessible ..., :phone

For Rails 4 see answer of the OP.

Upvotes: 1

Tuyen Nguyen
Tuyen Nguyen

Reputation: 4479

So I followed Deefour's comment and update the app/controllers/tickets_controller.rb file.
I only modified the ticket_params method to add the phone field, everything is working now.

BEFORE

# Never trust parameters from the scary internet, only allow the white list through.
    def ticket_params
      params.require(:ticket).permit(:name, :seat_id_seq, :address, :price_paid, :email_address)
    end

AFTER

# Never trust parameters from the scary internet, only allow the white list through.
    def ticket_params
      params.require(:ticket).permit(:name, :seat_id_seq, :address, :price_paid, :email_address, :phone)
    end

Upvotes: 1

Related Questions