新指定列的未定义方法

时间:2013-02-28 03:41:51

标签: ruby-on-rails

我必须创建一个新的迁移,将“英尺”和“英寸”添加到个人资料页面,以便用户输入他们的身高。我跑了命令:

  

rails生成迁移AddFeetAndInchesToUsers脚:整数   寸:整数

认为这会让它发挥作用而事实并非如此。我收到错误消息 `

  

“#User的未定义方法`脚”:0x007fe41dbe9520“

`尝试查看个人资料页面时。

我已将所有其他字段用于我的个人资料页面,因为我之前通过“rails g resource user then-all-my-options-here”在用户模型中定义了它们。但现在我需要定义英尺,英寸,宗教和宗教的重要性。我不了解如何添加到现有模型。

我已将这些值添加到db文件夹中的create_profiles.rb。添加到profile.rb和user.rb.这些都不起作用。我相信要修复它我需要以某种方式在user.rb中以正确的方式进入它,因为我只是在attr_accessible中键入它并且我确定正确地获得“脚,选项和宗教选项”才能通过首先是终端。

User.rb

class User < ActiveRecord::Base
  has_secure_password
  attr_accessible :about_me, :feet, :inches, :password, :birthday, :career, :children, :education, :email, :ethnicity, :gender, :height, :name, :password_digest, :politics, :religion, :sexuality, :user_drink, :user_smoke, :username, :zip_code
  validates_uniqueness_of :email
  validates_presence_of :password, :on => :create
  before_create { generate_token(:auth_token) }

  def send_password_reset
    generate_token(:password_reset_token)
    self.password_reset_sent_at = Time.zone.now
    save!
    UserMailer.password_reset(self).deliver
  end

  def generate_token(column)
    begin
      self[column] = SecureRandom.urlsafe_base64
    end while User.exists?(column => self[column])
  end
end

show.html.erb(个人资料页面):

<h1><%= @user.username %></h1>

<h2>Basics</h2>

<%= form_for @user do |f| %>

    <div class="field">
        <%= f.label :height %><br/>
        <%= f.select :feet, [['Feet', nil], '4', '5', '6'] %>
        <%= f.select :inches, [['Inches', nil], '0', '1', '2', '3', '4',                    
                                '5', '6', '7', '8', '9', '10', '11'] %>
        </div>
    <div class="field">
        <%= f.label :children %><br/>
        <%= f.select :children, [['Do you have or want kids?', nil], 'Yes, they live with me', 'I want kids now', 'I want one someday', 'Not for me']%>
        </div>
    <div class="field">
        <%= f.label :religion %><br/>
        <%= f.select :religion, [['What is your faith?', nil], 'Agnostic', 'Atheist', 'Christian', 'Catholic', 'Buddhist', 'Hindu', 'Jewish', 'Muslim', 'Spiritual without affiliation', 'Other', 'None', 'Prefer not to say'  ]%><br/>
        <%= f.select :religion, [['How important is this to you?', nil], 'Very Important', 'Somewhat Important', 'Not Important']%>
        </div>
    <div class="field">
        <%= f.label :career %><br/>
        <%= f.text_field :career %>
    </div>
    <div class="field">
        <%= f.label :education %><br/>
        <%= f.select :education, [['What is your education level?', nil], 'High school', 'Some college', 'Undergraduate', "Bachelor's", "Master's ", 'PhD', 'Business school', 'Law school', 'Medical school' ]%>
        </div>
    <div class="field">
        <%= f.label :ethnicity %><br/>
        <%= f.select :ethnicity, [['What is your ethnicity?', nil], 'Asian', 'Black', 'Biracial', 'Indian', 'Hispanic/Latin', 'Middle Eastern', 'Native American', 'Pacific Islander', 'White', 'Other' ]%>
        </div>
        <%= f.label :user_drink %><br/>
        <%= f.select :user_drink, [['How much do you drink?', nil], 'Often Drinks', 'Sometimes drinks', 'Never drinks', 'No comment' ]%>
        </div><br/>
        <%= f.label :user_smoke %><br/>
        <%= f.select :user_smoke, [['How often do you smoke?', nil], 'Often smokes', 'Sometimes smokes', 'Never smokes'] %>
        </div>
    <div class="actions"><%= f.submit %></div>

    <h3>About Me</h3>

    <%= form_for @user do |f| %>

    <div class="field">
        <%= f.label :about_me %><br/>
        <%= f.text_field :about_me %>
    <div class="actions"><%= f.submit %></div>

<% end %>
<% end %>

add_feet_and_inches_to_users.rb(这是在/ db文件夹中,是在我发布帖子顶部发布的命令后创建的):

class AddFeetAndInchesToUsers < ActiveRecord::Migration
  def change
    add_column :users, :feet, :integer
    add_column :users, :inches, :integer
  end
end

1 个答案:

答案 0 :(得分:1)

我原本遵循了正确的步骤。阻碍我的是我尝试运行rake db:migrate时收到的错误。我有重复的列。所以为了找到副本,我运行了“grep password_digest db / migrate / *”。作为回报,我看到了包含重复内容的文件,我删除了它们,现在我的字段可以正常工作。