状态中的NoMethodError

时间:2013-06-09 05:46:06

标签: ruby-on-rails

更新:总的来说,我仍然试图摆脱下面的错误。我多次重新观看视频,试图找出可能出错的步骤。我删除了我的db / migrate AddUserIdtoStatuses并重做了这个。现在,当我rake db:migrate时,我的终端出现以下错误:

new-host:treebook ScottDAlessandro$ rake db:migrate
==  AddUserIdToStatuses: migrating ============================================
-- add_column(:statuses, :user_id, :integer)
rake aborted!
An error has occurred, this and all later migrations canceled:

SQLite3::SQLException: duplicate column name: user_id: ALTER TABLE "statuses" ADD "user_id" integer/usr/local/rvm/gems/ruby-1.9.3-p392/gems/sqlite3-1.3.7/lib/sqlite3/database.rb:91:in `initialize'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/sqlite3-1.3.7/lib/sqlite3/database.rb:91:in `new'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/sqlite3-1.3.7/lib/sqlite3/database.rb:91:in `prepare'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/sqlite3-1.3.7/lib/sqlite3/database.rb:134:in `execute'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.12/lib/active_record/connection_adapters/sqlite_adapter.rb:278:in `block in execute'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `block in log'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.12/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract_adapter.rb:275:in `log'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.12/lib/active_record/connection_adapters/sqlite_adapter.rb:278:in `execute'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract/schema_statements.rb:264:in `add_column'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.12/lib/active_record/connection_adapters/sqlite_adapter.rb:400:in `add_column'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.12/lib/active_record/migration.rb:466:in `block in method_missing'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.12/lib/active_record/migration.rb:438:in `block in say_with_time'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.12/lib/active_record/migration.rb:438:in `say_with_time'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.12/lib/active_record/migration.rb:458:in `method_missing'
/Users/ScottDAlessandro/code/treebook/db/migrate/20130610190852_add_user_id_to_statuses.rb:3:in `change'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.12/lib/active_record/migration.rb:407:in `block (2 levels) in migrate'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.12/lib/active_record/migration.rb:407:in `block in migrate'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract/connection_pool.rb:129:in `with_connection'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.12/lib/active_record/migration.rb:389:in `migrate'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.12/lib/active_record/migration.rb:528:in `migrate'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.12/lib/active_record/migration.rb:720:in `block (2 levels) in migrate'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.12/lib/active_record/migration.rb:775:in `call'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.12/lib/active_record/migration.rb:775:in `block in ddl_transaction'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.12/lib/active_record/transactions.rb:208:in `transaction'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.12/lib/active_record/migration.rb:775:in `ddl_transaction'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.12/lib/active_record/migration.rb:719:in `block in migrate'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.12/lib/active_record/migration.rb:700:in `each'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.12/lib/active_record/migration.rb:700:in `migrate'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.12/lib/active_record/migration.rb:570:in `up'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.12/lib/active_record/migration.rb:551:in `migrate'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.12/lib/active_record/railties/databases.rake:179:in `block (2 levels) in <top (required)>'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/task.rb:246:in `call'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/task.rb:246:in `block in execute'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/task.rb:241:in `each'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/task.rb:241:in `execute'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/task.rb:184:in `block in invoke_with_call_chain'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/task.rb:177:in `invoke_with_call_chain'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/task.rb:170:in `invoke'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/application.rb:143:in `invoke_task'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/application.rb:101:in `block (2 levels) in top_level'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/application.rb:101:in `each'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/application.rb:101:in `block in top_level'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/application.rb:110:in `run_with_threads'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/application.rb:95:in `top_level'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/application.rb:73:in `block in run'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/application.rb:160:in `standard_exception_handling'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/application.rb:70:in `run'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

我看到重复的列名称,这是否意味着我在删除文件之前“重做”我项目的一些最新工作,我没有删除SQL数据库中的所有内容?

我正在关注teamtreehouse.com上的Ruby on Rails指南。我一直遇到这个错误

NoMethodError in Statuses#new

Showing /Users/ScottDAlessandro/code/treebook/app/views/statuses/_form.html.erb where line #16 raised:

undefined method `name' for #<Status:0x00000107254ec0>
Extracted source (around line #16):

13: 
14:   <div class="field">
15:     <%= f.label :name %><br />
16:     <%= f.text_field :name %>
17:   </div>
18:   <div class="field">
19:     <%= f.label :content %><br />
Trace of template inclusion: app/views/statuses/new.html.erb

Rails.root: /Users/ScottDAlessandro/code/treebook

Application Trace | Framework Trace | Full Trace
app/views/statuses/_form.html.erb:16:in `block in _app_views_statuses__form_html_erb__871609707240624879_2174651900'
app/views/statuses/_form.html.erb:1:in `_app_views_statuses__form_html_erb__871609707240624879_2174651900'
app/views/statuses/new.html.erb:3:in `_app_views_statuses_new_html_erb__3089828772916649988_2174583640'
app/controllers/statuses_controller.rb:29:in `new'
Request

Parameters:

None
Show session dump

Show env dump

Response

Headers:

None

当我尝试更新页面上的状态时出现此错误。以下是我最近更新的一些其他文件。

**更新_form

<%= form_for(@status) do |f| %>
  <% if @status.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@status.errors.count, "error") %> prohibited this status from being saved:</h2>

      <ul>
      <% @status.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :content %><br />
    <%= f.text_area :content %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

分贝/迁移

class AddUserIdToStatuses < ActiveRecord::Migration
  def change
    add_column :statuses, :user_id, :integer
    add_index :statuses, :user_id
    remove_column :statuses, :name
  end
end

很抱歉描述不好,但我对这个错误的消息感到迷茫。当这个错误出现时,我不是百分之百,似乎整个项目中一切都进展顺利。有谁知道我可能在哪里出错或为什么我收到此错误?谢谢:))

4 个答案:

答案 0 :(得分:0)

full_name表上似乎没有statuses字段。您没有任何添加该字段的迁移,或者您有一个,但您没有针对您的数据库运行它。

或者,如果full_name应该是基于其他字段值的计算字段,那么您需要在full_name模型中添加Status方法。

答案 1 :(得分:0)

如果您使用form_for某个模型,则字段需要存在模型的属性。在这种情况下,您的status模型没有full_name属性,因此也是例外。

答案 2 :(得分:0)

我认为你遇到的问题(根据第15行)是,你有一个拼写错误,你需要使用fulll_name

答案 3 :(得分:0)

您的表单正在尝试为您的状态模型呈现name字段,但在上次迁移中,您刚刚删除了该字段。是name字段应该在表单上吗?您是否意外删除了name字段并需要将其放回数据库中?