Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/52.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 新指定列的未定义方法_Ruby On Rails - Fatal编程技术网

Ruby on rails 新指定列的未定义方法

Ruby on rails 新指定列的未定义方法,ruby-on-rails,Ruby On Rails,我必须创建一个新的迁移,将“英尺”和“英寸”添加到个人资料页面,以便用户可以输入他们的身高。我运行命令: rails生成迁移AddFeetAndInchesToUsers英尺:整数 英寸:整数 认为这会使它工作,但它没有。我正在接收错误消息 ` #用户:0x007fe41dbe9520的未定义方法“英尺” `尝试查看配置文件页面时 我已经为我的个人资料页面设置了所有其他字段,正如我之前在用户模型中通过“rails g resource user然后在此处设置所有选项”定义的那样。但现在我需要定义

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

rails生成迁移AddFeetAndInchesToUsers英尺:整数 英寸:整数

认为这会使它工作,但它没有。我正在接收错误消息 `

#用户:0x007fe41dbe9520的未定义方法“英尺”

`尝试查看配置文件页面时

我已经为我的个人资料页面设置了所有其他字段,正如我之前在用户模型中通过“rails g resource user然后在此处设置所有选项”定义的那样。但现在我需要定义脚、英寸、宗教和宗教的重要性。我不明白如何添加到现有模型中

我已将这些值添加到数据库文件夹中的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
class用户:create
在创建之前{generate_token(:auth_token)}
def发送密码重置
生成\u令牌(:密码\u重置\u令牌)
self.password\u reset\u sent\u at=Time.zone.now
拯救
UserMailer.password\u重置(self.deliver)
结束
def生成_令牌(列)
开始
self[column]=SecureRandom.urlsafe_base64
用户存在时结束?(列=>self[column])
结束
结束
show.html.erb(配置文件页):


基础










关于我
将_feet_和_inches_添加到_users.rb(这是在/db文件夹中,是在我运行张贴在帖子顶部的命令后创建的):

class addfeeandinchestousers
我最初遵循了正确的步骤。阻碍我的是我在尝试运行rake db:migrate时收到的错误。我有重复的列。因此,为了找到重复的密码,我运行了“grep password\u digest db/migrate/*”。作为回报,我向我展示了包含重复内容的文件,我删除了它们,现在我的字段开始工作。

第一个明显的步骤:是否运行了rake db:migrate?是的。耙子中止了。由于某种原因,当我输入该命令时,在尝试迁移之后,它还显示了“-add_column(:users,:password_digest,:string)”。当然已经创建了。因此,我在下面的“Mysql2::Error:重复列名'password_digest':ALTER TABLE
users
ADD
password_digest
varchar(255)”中收到了这个消息
<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 %>
class AddFeetAndInchesToUsers < ActiveRecord::Migration
  def change
    add_column :users, :feet, :integer
    add_column :users, :inches, :integer
  end
end