简体   繁体   中英

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM