0

I had to create a new migration to add "feet" and "inches" to the profile page so that users can enter their height. I ran the command:

rails generate migration AddFeetAndInchesToUsers feet:integer inches:integer

thinking that would get it to work and it didn't. I am receiving the error message `

"undefined method `feet' for #User:0x007fe41dbe9520"

` when trying to view profile page.

I have all the other fields working for my profile page as I have defined them in the user model previously by doing "rails g resource user then-all-my-options-here". But now I need to define feet, inches, religion and religion importance. I'm not understanding how to add to an existing model.

I have added those values to the create_profiles.rb which is in the db folder. Added to profile.rb and user.rb. None of those work. I believe to fix it I need to somehow get it inside user.rb the proper way, as I only typed it into the attr_accessible and I'm sure to properly get "feet, options, and religion options" to work is to go through the terminal first.

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 (profile page):

<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 (this is in /db folder and was created after I ran the command posted on top of post):

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

1 回答 1

1

我最初遵循了正确的步骤。让我感到困惑的是我在尝试运行 rake db:migrate 时收到的错误。我有重复的列。因此,为了找到重复项,我运行了“grep password_digest db/migrate/*”。作为回报,我向我展示了包含重复内容的文件,我删除了它们,现在我的字段可以工作了。

于 2013-02-28T12:14:58.657 回答