【问题标题】:Why am I getting a No Method Error in my rails app?为什么我的 rails 应用程序出现无方法错误?
【发布时间】:2011-09-09 09:20:03
【问题描述】:

我知道我是这方面的新手,而且我不值得,但有人可以向我解释一下为什么我会收到无方法错误吗?这就是我所做的。我为我的数据库生成了一个新的迁移到我现有的 rails 应用程序,迁移称为“配置文件”。我运行了 db:migrate,然后继续编辑我以前的“new.html.erb”表单。代码如下所示:

class CreateProfiles < ActiveRecord::Migration
  def self.up
    create_table :profiles do |t|
      t.string :major
      t.string :year
      t.string :books_sell
      t.string :books_buy
      t.string :facebook
      t.string :restaurants
      t.string :interests

      t.timestamps
    end
    add_index :profiles, :major
    add_index :profiles, :year
    add_index :profiles, :books_sell
    add_index :profiles, :books_buy
    add_index :profiles, :facebook
    add_index :profiles, :restaurants
    add_index :profiles, :interests
  end

  def self.down
    drop_table :profiles
  end
end

基本上,我在我的应用程序中添加了一个配置文件部分,但我得到了这个:

   undefined method `major' for #<User:0x00000100b6e030>
    Extracted source (around line #23):

    20:   </div>
    21:   <div class="field">
    22:     <%= f.label :"major" %><br />
    23:     <%= f.text_field :major %>
    24:   </div>

这是我的意见/用户/new.hmtl.erb 文件:

<h1>Sign up</h1>

<%= form_for(@user) do |f| %>
  <%= render 'shared/error_messages', :object => f.object %>
  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :email %><br />
    <%= f.text_field :email %>
  </div>
  <div class="field">
    <%= f.label :password %><br />
    <%= f.password_field :password %>
  </div>
  <div class="field">
    <%= f.label :password_confirmation, "Confirmation" %><br />
    <%= f.password_field :password_confirmation %> 
  </div>
  <div class="field">
    <%= f.label :"year" %><br />
    <%= f.text_field :year %>
  </div>
  <div class="field">
    <%= f.label :"major" %><br />
    <%= f.text_field :major %>
  </div>
  <div class="actions">
    <%= f.submit "Sign up" %>
  </div>
<% end %>

缺少什么?

【问题讨论】:

  • 您的表单适用于 User 模型,而您的迁移适用于 Profile 模型。这可能是你的问题吗?
  • @Benoit Garret,我认为你说得对。我会调查的。
  • 说真的,不要为自己是新手而道歉。阅读这个关于新手问题的帖子:meta.stackexchange.com/questions/97327/…
  • @Benoit Garret。感谢您的建议和链接。前几天我因为发布“新手”问题而被骂。顺便说一句,我想通了,并将在某处发布答案! :)
  • 你可以在这里发布并接受你自己的答案;-)

标签: database ruby-on-rails-3 migration nomethoderror


【解决方案1】:

这里的问题是我之前在用户模型下完成了一个表单视图。我想加入那个表格,所以我创建了一个名为 profile 的新迁移。我这样做是因为我无法手动回滚用户模型的迁移,而只能添加字符串和列。

但是,从用户模型下的配置文件模型中添加文本字段会产生错误。

我所做的是创建了一个Add_xxx_to_yyy 迁移,它允许我将列添加到先前创建的迁移中而不会出现任何问题。我使用带有下划线的rails generate migration Add_profile_to_User,因为我使用的是rails 3.0(当我使用Addprofiletouser 时它不起作用)。等等!

【讨论】:

  • 您可以使用rake db:rollback STEP=n 回滚迁移,其中n 是您要回滚的迁移数量。我总是在开发时修改现有的迁移,使用回滚+迁移。
  • 至于迁移生成,您可以指定您的迁移名称为驼峰式 (AddProfileToUser) 或蛇式 (add_profile_to_user)。
【解决方案2】:

您已粘贴 Profile 模型的迁移。而且我猜,在您的 @user 变量中,您有 User 模型的新实例。

由于没有为您的用户定义方法或属性major,您会看到投诉“未定义的方法...”

【讨论】:

    猜你喜欢
    • 2013-09-15
    • 1970-01-01
    • 1970-01-01
    • 2013-10-17
    • 1970-01-01
    • 2021-11-08
    • 2018-04-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多