0

I have a t.string in my db migration and schema called description. Users can add a description to a picture. I also would like to add a text field, how should I go about to do that?

Here is my view for the description (would like to have a similar view for the text field):

  <div class="form-group has-error">
          <%= f.label :Title %><br>
          <%= f.text_field :description, class: "form-control" %>
  </div>

Here is my schema:

 ActiveRecord::Schema.define(version: 20140715054507) do


      create_table "pins", force: true do |t|
        t.string   "description"
        t.datetime "created_at"
        t.datetime "updated_at"
        t.integer  "user_id"
        t.string   "image_file_name"
        t.string   "image_content_type"
        t.integer  "image_file_size"
        t.datetime "image_updated_at"
        t.string   "document_file_name"
        t.string   "document_content_type"
        t.integer  "document_file_size"
        t.datetime "document_updated_at"
  end

Here is my Pin controller :

class CreatePins < ActiveRecord::Migration
  def change
    create_table :pins do |t|
      t.string :description


      t.timestamps
    end
  end
end

Lastly, do I need to create a whole new migration for the text field? Such as AddTextToPin for example then run rake db:migrate to add the line to my schema

Heroku Logs

heroku logs
2014-07-18T23:10:06.734455+00:00 app[web.1]: Completed 200 OK in 479ms (Views: 243.7ms | ActiveRecord: 83.7ms)
2014-07-18T23:10:06.733785+00:00 app[web.1]:   Rendered layouts/_header.html.erb (6.5ms)
2014-07-18T23:10:07.239143+00:00 heroku[router]: at=info method=GET path="/assets/application-24d698747259b0560e4ffd846de615a9.css" host=cryptic-gorge-7497.herokuapp.com request_id=a6c175d7-a59c-4527-9d62-33f769ba1515 fwd="65.78.4.236" dyno=web.1 connect=3ms service=22ms status=304 bytes=133
2014-07-18T23:10:08.038093+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=cryptic-gorge-7497.herokuapp.com request_id=d75e4c7e-14c1-4e24-a3d7-5cec328499c4 fwd="65.78.4.236" dyno=web.1 connect=2ms service=5ms status=200 bytes=228
2014-07-18T23:10:16.712438+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=cryptic-gorge-7497.herokuapp.com request_id=486c8c95-a788-452f-92e2-86bb416e24e5 fwd="65.78.4.236" dyno=web.1 connect=1ms service=103ms status=304 bytes=133
2014-07-18T23:10:16.716988+00:00 heroku[router]: at=info method=GET path="/pins/new" host=cryptic-gorge-7497.herokuapp.com request_id=bd7cd294-8c25-44fe-889e-b6f7d46d8983 fwd="65.78.4.236" dyno=web.1 connect=1ms service=186ms status=500 bytes=1754
2014-07-18T23:10:16.540286+00:00 app[web.1]: Started GET "/pins/new" for 65.78.4.236 at 2014-07-18 23:10:16 +0000
2014-07-18T23:10:16.694356+00:00 app[web.1]:   Rendered pins/_form.html.erb (26.4ms)
2014-07-18T23:10:16.694416+00:00 app[web.1]:   Rendered pins/new.html.erb within layouts/application (27.9ms)
2014-07-18T23:10:16.705528+00:00 app[web.1]: ActionView::Template::Error (undefined method `description_text' for #<Pin:0x007f1f24a42e60>):
2014-07-18T23:10:16.705531+00:00 app[web.1]:     36:         <div class="form-group has-error">
2014-07-18T23:10:16.705532+00:00 app[web.1]:     37:           <%= f.label :Notes %><br>
2014-07-18T23:10:16.694579+00:00 app[web.1]: Completed 500 Internal Server Error in 147ms
2014-07-18T23:10:16.705524+00:00 app[web.1]: 
2014-07-18T23:10:16.705534+00:00 app[web.1]:     38:           <%= f.text_area :description_text, class: "form-control" %>
2014-07-18T23:10:16.705539+00:00 app[web.1]:   app/views/pins/_form.html.erb:38:in `block in _app_views_pins__form_html_erb___884995543155172252_69885869808520'
2014-07-18T23:10:16.705543+00:00 app[web.1]: 
2014-07-18T23:10:16.705530+00:00 app[web.1]:     35: 
2014-07-18T23:10:16.705542+00:00 app[web.1]:   app/views/pins/new.html.erb:16:in `_app_views_pins_new_html_erb___1023639770328481838_69885867942860'
2014-07-18T23:10:16.705535+00:00 app[web.1]:     39:         </div>
2014-07-18T23:10:16.772564+00:00 app[web.1]: Started GET "/pins/new" for 65.78.4.236 at 2014-07-18 23:10:16 +0000
2014-07-18T23:10:16.705544+00:00 app[web.1]: 
2014-07-18T23:10:16.705537+00:00 app[web.1]:     40:      </div>
2014-07-18T23:10:16.705538+00:00 app[web.1]:     41:   </div>
2014-07-18T23:10:16.705541+00:00 app[web.1]:   app/views/pins/_form.html.erb:1:in `_app_views_pins__form_html_erb___884995543155172252_69885869808520'
2014-07-18T23:10:16.547570+00:00 app[web.1]: Processing by PinsController#new as HTML
2014-07-18T23:10:16.776609+00:00 app[web.1]: Processing by PinsController#new as HTML
2014-07-18T23:10:16.818442+00:00 heroku[router]: at=info method=GET path="/pins/new" host=cryptic-gorge-7497.herokuapp.com request_id=d53c1f5b-df98-4a12-bb84-0280d666dd95 fwd="65.78.4.236" dyno=web.1 connect=1ms service=49ms status=500 bytes=1754
2014-07-18T23:10:16.807965+00:00 app[web.1]:   Rendered pins/_form.html.erb (10.6ms)
2014-07-18T23:10:16.811064+00:00 app[web.1]: Completed 500 Internal Server Error in 34ms
2014-07-18T23:10:16.813126+00:00 app[web.1]: 
2014-07-18T23:10:16.813130+00:00 app[web.1]: ActionView::Template::Error (undefined method `description_text' for #<Pin:0x007f1f2758b8d8>):
2014-07-18T23:10:16.810825+00:00 app[web.1]:   Rendered pins/new.html.erb within layouts/application (14.4ms)
2014-07-18T23:10:16.813133+00:00 app[web.1]:     36:         <div class="form-group has-error">
2014-07-18T23:10:16.813131+00:00 app[web.1]:     35: 
2014-07-18T23:10:16.813134+00:00 app[web.1]:     37:           <%= f.label :Notes %><br>
2014-07-18T23:10:16.813135+00:00 app[web.1]:     38:           <%= f.text_area :description_text, class: "form-control" %>
2014-07-18T23:10:16.813136+00:00 app[web.1]:     39:         </div>
2014-07-18T23:10:16.813138+00:00 app[web.1]:     40:      </div>
2014-07-18T23:10:16.813139+00:00 app[web.1]:     41:   </div>
2014-07-18T23:10:16.813140+00:00 app[web.1]:   app/views/pins/_form.html.erb:38:in `block in _app_views_pins__form_html_erb___884995543155172252_69885891209480'
2014-07-18T23:10:16.813141+00:00 app[web.1]:   app/views/pins/_form.html.erb:1:in `_app_views_pins__form_html_erb___884995543155172252_69885891209480'
2014-07-18T23:10:16.813143+00:00 app[web.1]:   app/views/pins/new.html.erb:16:in `_app_views_pins_new_html_erb___1023639770328481838_69885891095460'
2014-07-18T23:10:16.813144+00:00 app[web.1]: 
2014-07-18T23:10:16.813145+00:00 app[web.1]: 
2014-07-18T23:10:34.546133+00:00 heroku[api]: Starting process with command `bundle exec rake db:migrate` by cyzanfar@gmail.com
2014-07-18T23:10:38.033734+00:00 heroku[run.4015]: State changed from starting to up
2014-07-18T23:10:37.863840+00:00 heroku[run.4015]: Awaiting client
2014-07-18T23:10:37.896815+00:00 heroku[run.4015]: Starting process with command `bundle exec rake db:migrate`
2014-07-18T23:10:42.134945+00:00 heroku[run.4015]: Process exited with status 0
2014-07-18T23:10:42.147504+00:00 heroku[run.4015]: State changed from up to complete
2014-07-18T23:10:49.939313+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=cryptic-gorge-7497.herokuapp.com request_id=e9257c3a-b1b8-4656-9238-9e1ee9fb5059 fwd="65.78.4.236" dyno=web.1 connect=1ms service=5ms status=304 bytes=133
2014-07-18T23:10:52.146162+00:00 heroku[router]: at=info method=GET path="/" host=cryptic-gorge-7497.herokuapp.com request_id=5353d49f-0975-4d20-bc69-0eedd95a1384 fwd="65.78.4.236" dyno=web.1 connect=1ms service=112ms status=304 bytes=885
2014-07-18T23:10:52.042820+00:00 app[web.1]: Started GET "/" for 65.78.4.236 at 2014-07-18 23:10:52 +0000
2014-07-18T23:10:52.136875+00:00 app[web.1]:   Rendered pins/index.html.erb within layouts/application (72.1ms)
2014-07-18T23:10:52.138573+00:00 app[web.1]: Completed 200 OK in 90ms (Views: 62.1ms | ActiveRecord: 12.1ms)
2014-07-18T23:10:52.138130+00:00 app[web.1]:   Rendered layouts/_header.html.erb (0.5ms)
2014-07-18T23:10:52.048059+00:00 app[web.1]: Processing by PinsController#index as HTML
2014-07-18T23:10:52.350842+00:00 heroku[router]: at=info method=GET path="/assets/application-24d698747259b0560e4ffd846de615a9.css" host=cryptic-gorge-7497.herokuapp.com request_id=42b585f3-58ac-46c0-b65a-95613556705c fwd="65.78.4.236" dyno=web.1 connect=1ms service=5ms status=304 bytes=133
2014-07-18T23:10:52.368420+00:00 heroku[router]: at=info method=GET path="/assets/application-962b1243754d8b7f620b3e30c0bcf81c.js" host=cryptic-gorge-7497.herokuapp.com request_id=620314b3-4164-484c-aee1-3c3dcfefd0ba fwd="65.78.4.236" dyno=web.1 connect=3ms service=9ms status=304 bytes=133
2014-07-18T23:10:52.728299+00:00 heroku[router]: at=info method=GET path="/assets/bootstrap/glyphicons-halflings-regular-8921a93349df81eec3d107b946cadc11.woff" host=cryptic-gorge-7497.herokuapp.com request_id=f5144646-28bf-4fb8-a3bb-9578c58f1052 fwd="65.78.4.236" dyno=web.1 connect=6ms service=23ms status=304 bytes=133
2014-07-18T23:10:59.189629+00:00 heroku[router]: at=info method=GET path="/pins/new" host=cryptic-gorge-7497.herokuapp.com request_id=62c16169-bdf6-4016-9011-13dbafceffb9 fwd="65.78.4.236" dyno=web.1 connect=1ms service=57ms status=500 bytes=1754
2014-07-18T23:10:59.253013+00:00 heroku[router]: at=info method=GET path="/pins/new" host=cryptic-gorge-7497.herokuapp.com request_id=7d743da7-1ddd-488f-afe4-a6ac4c30837b fwd="65.78.4.236" dyno=web.1 connect=1ms service=27ms status=500 bytes=1754
2014-07-18T23:10:59.173820+00:00 app[web.1]:   Rendered pins/_form.html.erb (16.5ms)
2014-07-18T23:10:59.135009+00:00 app[web.1]: Started GET "/pins/new" for 65.78.4.236 at 2014-07-18 23:10:59 +0000
2014-07-18T23:10:59.235238+00:00 app[web.1]: Processing by PinsController#new as HTML
2014-07-18T23:10:59.142489+00:00 app[web.1]: Processing by PinsController#new as HTML
2014-07-18T23:10:59.183142+00:00 app[web.1]: 
2014-07-18T23:10:59.183147+00:00 app[web.1]: ActionView::Template::Error (undefined method `description_text' for #<Pin:0x007f1f28515240>):
2014-07-18T23:10:59.179167+00:00 app[web.1]:   Rendered pins/new.html.erb within layouts/application (17.1ms)
2014-07-18T23:10:59.183149+00:00 app[web.1]:     35: 
2014-07-18T23:10:59.183150+00:00 app[web.1]:     36:         <div class="form-group has-error">
2014-07-18T23:10:59.183157+00:00 app[web.1]:     41:   </div>
2014-07-18T23:10:59.183161+00:00 app[web.1]:   app/views/pins/new.html.erb:16:in `_app_views_pins_new_html_erb___1023639770328481838_69885867942860'
2014-07-18T23:10:59.179180+00:00 app[web.1]: Completed 500 Internal Server Error in 32ms
2014-07-18T23:10:59.183152+00:00 app[web.1]:     37:           <%= f.label :Notes %><br>
2014-07-18T23:10:59.183163+00:00 app[web.1]: 
2014-07-18T23:10:59.246971+00:00 app[web.1]: 
2014-07-18T23:10:59.183154+00:00 app[web.1]:     38:           <%= f.text_area :description_text, class: "form-control" %>
2014-07-18T23:10:59.183156+00:00 app[web.1]:     40:      </div>
2014-07-18T23:10:59.183160+00:00 app[web.1]:   app/views/pins/_form.html.erb:1:in `_app_views_pins__form_html_erb___884995543155172252_69885869808520'
2014-07-18T23:10:59.183164+00:00 app[web.1]: 
2014-07-18T23:10:59.183159+00:00 app[web.1]:   app/views/pins/_form.html.erb:38:in `block in _app_views_pins__form_html_erb___884995543155172252_69885869808520'
2014-07-18T23:10:59.246975+00:00 app[web.1]:     35: 
2014-07-18T23:10:59.247001+00:00 app[web.1]:     41:   </div>
2014-07-18T23:10:59.247005+00:00 app[web.1]:   app/views/pins/new.html.erb:16:in `_app_views_pins_new_html_erb___1023639770328481838_69885891095460'
2014-07-18T23:10:59.226387+00:00 app[web.1]: Started GET "/pins/new" for 65.78.4.236 at 2014-07-18 23:10:59 +0000
2014-07-18T23:10:59.244374+00:00 app[web.1]:   Rendered pins/new.html.erb within layouts/application (4.6ms)
2014-07-18T23:10:59.246977+00:00 app[web.1]:     36:         <div class="form-group has-error">
2014-07-18T23:10:59.246999+00:00 app[web.1]:     40:      </div>
2014-07-18T23:10:59.244292+00:00 app[web.1]:   Rendered pins/_form.html.erb (4.4ms)
2014-07-18T23:10:59.244537+00:00 app[web.1]: Completed 500 Internal Server Error in 9ms
2014-07-18T23:10:59.246998+00:00 app[web.1]:     39:         </div>
2014-07-18T23:10:59.247002+00:00 app[web.1]:   app/views/pins/_form.html.erb:38:in `block in _app_views_pins__form_html_erb___884995543155172252_69885891209480'
2014-07-18T23:10:59.247003+00:00 app[web.1]:   app/views/pins/_form.html.erb:1:in `_app_views_pins__form_html_erb___884995543155172252_69885891209480'
2014-07-18T23:10:59.246997+00:00 app[web.1]:     38:           <%= f.text_area :description_text, class: "form-control" %>
2014-07-18T23:10:59.183155+00:00 app[web.1]:     39:         </div>
2014-07-18T23:10:59.246974+00:00 app[web.1]: ActionView::Template::Error (undefined method `description_text' for #<Pin:0x007f1f285b1438>):
2014-07-18T23:10:59.246995+00:00 app[web.1]:     37:           <%= f.label :Notes %><br>
2014-07-18T23:10:59.247007+00:00 app[web.1]: 
2014-07-18T23:10:59.247006+00:00 app[web.1]:
Cyzanfar
  • 6,997
  • 9
  • 43
  • 81

1 Answers1

3

You never need to touch your schema. Just create a new migration and add the text field there. Generate the migration like this

rails generate migration add_description_text_to_pins

Then open your new migration file and add the field like this.

class AddDescriptionTextToPins < ActiveRecord::Migration
  def change
    add_column :pins, :description_text, :text
  end
end

When you add a column to a table through a migration you use the add_column method. It takes 3 parameters. The table to be added to, the new column name, and the column type.

Now in you view you can do something like this

<div class="form-group has-error">
      <%= f.label :Title %><br>
      <%= f.text_field :description, class: "form-control" %>
      <%= f.label :description %><br>
      <%= f.text_area :description_text, class: "form-control" %>
</div>
Doug Steinberg
  • 1,122
  • 14
  • 32
  • Did everything you told me and then ran `rake db:migrate` to add the new text description to my schema. However, when i run`rake db:migrate` It's not adding the right migration AddDescriptionTextToPins its trying to add another one and therefore I get a duplicate error :`StandardError: An error has occurred, this and all later migrations canceled: SQLite3::SQLException: duplicate column name: document_file_name: ALTER TABLE "pins" ADD "document_file_name" ` – Cyzanfar Jul 18 '14 at 22:14
  • 1
    The migrations should have timestamps in the file names which prevent migrations from being run twice. Do you have GitHub repository for this so I can see the rest of your files? – Doug Steinberg Jul 18 '14 at 22:23
  • Yes here it is https://github.com/cyzanfar/Photoshare. When I run `rake db:migrate` instead of adding the new file `AddDescriptionTextToPins` its trying to add another one I created earlier called `AddFileToPin` – Cyzanfar Jul 18 '14 at 22:26
  • I just cloned your app locally and got it up and running. The migrations all ran cleanly. You're database might be messed up. You can do `rake db:reset` or `rake db:drop` and then migrate again. There is a SO post about it here. http://stackoverflow.com/questions/4116067/purge-or-recreate-a-ruby-on-rails-database – Doug Steinberg Jul 18 '14 at 22:44
  • Excellent! I ran `rake db:reset` and it worked thank you! How come it was not working when I tried running `rake db:migrate` earlier. Thank you, I still got a lot of work to do on it. – Cyzanfar Jul 18 '14 at 23:02
  • 1
    in your pins_controller.rb file on line 55 you have this `params.require(:pin).permit(:description, :image, :document)` you need to add `:description_text` to it to account for the new column. So it should look like this `params.require(:pin).permit(:description, :image, :document, :description_text)` This is called strong params. It a rails thing for only allowing the specified form fields to be passed into the database. I'm not sure if that will fix the problem, but it definitely needs to be fixed. – Doug Steinberg Jul 19 '14 at 00:33