将t.text添加到表模式RoR

时间:2014-07-18 19:04:13

标签: ruby-on-rails schema

我的数据库迁移架构中的t.string称为描述。用户可以向图片添加描述。我还想添加一个文本字段,我该怎么做呢?

以下是我对描述的看法(希望对文本字段有类似的观点):

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

这是我的架构:

 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

这是我的Pin控制器:

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


      t.timestamps
    end
  end
end

最后,我是否需要为文本字段创建全新的迁移?例如AddTextToPin例如然后运行rake db:migrate将行添加到我的架构

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]:

1 个答案:

答案 0 :(得分:1)

您永远不需要触摸您的架构。只需创建一个新的迁移并在那里添加文本字段。像这样生成迁移

rails generate migration add_description_text_to_pins

然后打开新的迁移文件并添加如下字段。

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

通过迁移向表中添加列时,可以使用add_column方法。它需要3个参数。要添加的表,新列名称和列类型。

现在在你看来你可以做这样的事情

<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>
相关问题