ActionView :: Template :: Error(未定义的方法`phone&#39; for#<user:0x007f35612a5448>):</user:0x007f35612a5448>

时间:2015-02-04 12:10:55

标签: ruby-on-rails ruby ruby-on-rails-4 heroku railstutorial.org

我完成了为我的新rails应用程序创建用户注册,一切都在我的本地计算机上正常开发。但是在heroku上,部署的应用程序将显示主页和大多数其他页面,但signup_path除外,它们会呈现&#34; new.html.erb&#34;。单击此路径会给我一个错误,告诉我检查我的日志。

我已经检查了日志并尝试过几件事,但现在还不知道该怎么做。以下是日志:http://pastebin.com/v1fVqLbL

这里的主页工作正常,但是附加到&#34;的路径是&#34;没有按&#39;吨

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

我的生产架构,我相信电话已定义:

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最新教程中的最终gem文件。不知道还能做什么。

2 个答案:

答案 0 :(得分:1)

他们在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' %>

答案 1 :(得分:1)

我的问题在于迁移。我仍然不明白为什么迁移不能正常工作,但我能够在5分钟内通过删除heroku应用程序并开始这样一个新的修复此问题:

heroku apps:destroy -a appname
heroku create appname
git push heroku master
heroku run rake db:migrate

对于任何正在进行Hartl教程的人或者一般来说都是rails的新手,如果您认为迁移是您的问题并且您无法修复当前的heroku应用程序,我强烈建议您尝试这样做。您当前的应用程序中不太可能有任何东西推送到heroku并且再次迁移将不会复制。