ActionView::Template::Error(#<User:0x007f35612a5448> 的未定义方法“phone”):
ActionView::Template::Error (undefined method `phone' for #<User:0x007f35612a5448>):
我完成了为我的新 rails 应用程序创建用户注册,并且在我的本地计算机上开发时一切正常。但是在 heroku 上,部署的应用程序将显示主页和大多数其他页面,除了呈现 "new.html.erb" 的 signup_path。单击此路径会提示我检查日志。
我已经检查了日志并尝试了几件事,但现在不知道该怎么做。以下是日志:http://pastebin.com/v1fVqLbL
这是可以正常工作的主页,但是附加到 "sign in" 的路径不正常
home.html.erb
<% provide(:title, "Home") %>
<div class="center jumbotron">
<h1>sample app</h1>
<%= link_to "Sign up now!", signup_path, class: "btn btn-lg btn-primary" %>
</div>
routes.rb
Rails.application.routes.draw do
get 'users/new'
root 'static_pages#home'
get 'help' => 'static_pages#help'
get 'about' => 'static_pages#about'
get 'contact' => 'static_pages#contact'
get 'signup' => 'users#new'
resources :users
end
我应该加载的注册页面:
new.html.erb
<% provide(:title, 'Sign up') %>
<h1>Sign up</h1>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for(@user) do |f| %>
<%= render 'shared/error_messages' %>
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
<%= f.label :email %>
<%= f.email_field :email, class: 'form-control' %>
<%= f.label :phone, "Phone Number" %>
<%= f.phone_field :phone, class: 'form-control' %>
<%= f.label :password %>
<%= f.password_field :password, class: 'form-control' %>
<%= f.label :password_confirmation, "Confirm Your Password" %>
<%= f.password_field :password_confirmation, class: 'form-control' %>
<%= f.submit "Create my account", class: "btn btn-primary" %>
<% end %>
</div>
</div>
Application.html.erb
<!DOCTYPE html>
<html>
<head>
<title><%= full_title(yield(:title)) %></title>
<%= stylesheet_link_tag "application", media: "all",
"data-turbolinks-track" => true %>
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
<%= csrf_meta_tags %>
<%= render 'layouts/shim' %>
</head>
<body>
<%= render 'layouts/header' %>
<div class="container">
<% flash.each do |message_type, message| %>
<div class="alert alert-<%= message_type %>"><%= message %></div>
<% end %>
<%= yield %>
<%= render 'layouts/footer' %>
<%= debug(params) if Rails.env.development? %>
</div>
</body>
</html>
user.rb
class User < ActiveRecord::Base
before_save { self.email = email.downcase }
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
VALID_PHONE_REGEX = /\A(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}\z/
validates :phone, presence: true, length: {maximum: 15},
format: { with: VALID_PHONE_REGEX },
uniqueness: true
has_secure_password
validates :password, length: { minimum: 6 }
end
我认为 phone 定义的生产架构:
ActiveRecord::Schema.define(version: 20150204094519) do
create_table "users", force: :cascade do |t|
t.string "name"
t.string "email"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "password_digest"
t.string "phone"
end
add_index "users", ["email"], name: "index_users_on_email", unique: true
add_index "users", ["phone"], name: "index_users_on_phone", unique: true
end
Heroku 迁移:
20150204074511_create_users.rb 20150204093042_add_phone_number_to_users.rb
20150204081616_add_index_to_users_email.rb 20150204094519_add_index_to_users_phone_number.rb
20150204081750_add_password_digest_to_users.rb
如有必要,很高兴添加任何其他文件。我是 运行 Hartl 最新教程中的最终 gemfile。不知道还能做什么。
他们在 rails 中没有这样的 phone_field,将其更改为 text_field 或 number_field 或 telephone_field。
<%= f.phone_field :phone, class: 'form-control' %>
至
<%= f.text_field :phone, class: 'form-control' %>
或
<%= f.telephone_field :phone, class: 'form-control' %>
我的问题是迁移。我仍然不明白为什么迁移不能正常工作,但我通过删除 heroku 应用程序并启动一个新应用程序,在 5 分钟内解决了这个问题:
heroku apps:destroy -a appname
heroku create appname
git push heroku master
heroku run rake db:migrate
对于学习 Hartl 教程的任何人或一般来说 rails 的新手,如果您认为迁移是您的问题并且您无法修复当前的 heroku 应用程序,我强烈建议您尝试此方法。您当前的应用程序中不太可能有任何推送到 heroku 并再次迁移不会复制的内容。
我完成了为我的新 rails 应用程序创建用户注册,并且在我的本地计算机上开发时一切正常。但是在 heroku 上,部署的应用程序将显示主页和大多数其他页面,除了呈现 "new.html.erb" 的 signup_path。单击此路径会提示我检查日志。
我已经检查了日志并尝试了几件事,但现在不知道该怎么做。以下是日志:http://pastebin.com/v1fVqLbL
这是可以正常工作的主页,但是附加到 "sign in" 的路径不正常
home.html.erb
<% provide(:title, "Home") %>
<div class="center jumbotron">
<h1>sample app</h1>
<%= link_to "Sign up now!", signup_path, class: "btn btn-lg btn-primary" %>
</div>
routes.rb
Rails.application.routes.draw do
get 'users/new'
root 'static_pages#home'
get 'help' => 'static_pages#help'
get 'about' => 'static_pages#about'
get 'contact' => 'static_pages#contact'
get 'signup' => 'users#new'
resources :users
end
我应该加载的注册页面:
new.html.erb
<% provide(:title, 'Sign up') %>
<h1>Sign up</h1>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for(@user) do |f| %>
<%= render 'shared/error_messages' %>
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
<%= f.label :email %>
<%= f.email_field :email, class: 'form-control' %>
<%= f.label :phone, "Phone Number" %>
<%= f.phone_field :phone, class: 'form-control' %>
<%= f.label :password %>
<%= f.password_field :password, class: 'form-control' %>
<%= f.label :password_confirmation, "Confirm Your Password" %>
<%= f.password_field :password_confirmation, class: 'form-control' %>
<%= f.submit "Create my account", class: "btn btn-primary" %>
<% end %>
</div>
</div>
Application.html.erb
<!DOCTYPE html>
<html>
<head>
<title><%= full_title(yield(:title)) %></title>
<%= stylesheet_link_tag "application", media: "all",
"data-turbolinks-track" => true %>
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
<%= csrf_meta_tags %>
<%= render 'layouts/shim' %>
</head>
<body>
<%= render 'layouts/header' %>
<div class="container">
<% flash.each do |message_type, message| %>
<div class="alert alert-<%= message_type %>"><%= message %></div>
<% end %>
<%= yield %>
<%= render 'layouts/footer' %>
<%= debug(params) if Rails.env.development? %>
</div>
</body>
</html>
user.rb
class User < ActiveRecord::Base
before_save { self.email = email.downcase }
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
VALID_PHONE_REGEX = /\A(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}\z/
validates :phone, presence: true, length: {maximum: 15},
format: { with: VALID_PHONE_REGEX },
uniqueness: true
has_secure_password
validates :password, length: { minimum: 6 }
end
我认为 phone 定义的生产架构:
ActiveRecord::Schema.define(version: 20150204094519) do
create_table "users", force: :cascade do |t|
t.string "name"
t.string "email"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "password_digest"
t.string "phone"
end
add_index "users", ["email"], name: "index_users_on_email", unique: true
add_index "users", ["phone"], name: "index_users_on_phone", unique: true
end
Heroku 迁移:
20150204074511_create_users.rb 20150204093042_add_phone_number_to_users.rb
20150204081616_add_index_to_users_email.rb 20150204094519_add_index_to_users_phone_number.rb
20150204081750_add_password_digest_to_users.rb
如有必要,很高兴添加任何其他文件。我是 运行 Hartl 最新教程中的最终 gemfile。不知道还能做什么。
他们在 rails 中没有这样的 phone_field,将其更改为 text_field 或 number_field 或 telephone_field。
<%= f.phone_field :phone, class: 'form-control' %>
至
<%= f.text_field :phone, class: 'form-control' %>
或
<%= f.telephone_field :phone, class: 'form-control' %>
我的问题是迁移。我仍然不明白为什么迁移不能正常工作,但我通过删除 heroku 应用程序并启动一个新应用程序,在 5 分钟内解决了这个问题:
heroku apps:destroy -a appname
heroku create appname
git push heroku master
heroku run rake db:migrate
对于学习 Hartl 教程的任何人或一般来说 rails 的新手,如果您认为迁移是您的问题并且您无法修复当前的 heroku 应用程序,我强烈建议您尝试此方法。您当前的应用程序中不太可能有任何推送到 heroku 并再次迁移不会复制的内容。