配置文件中没有方法错误

时间:2017-03-16 15:51:49

标签: ruby-on-rails

我有一个Rails应用程序,可以让您在注册后单击“创建用户配置文件”链接创建配置文件。但由于某种原因,下面的错误显示而不是我的表单字段页面。

编辑: 提出错误:

undefined method `first_name' for #<Profile id: nil>

显示/home/ubuntu/workspace/saasapp/app/views/profiles/_form.html.erb:

  <div class="form-group">
    <%= f.label :first_name %>
    <%= f.text_field :first_name, class: 'form-control' %>
  </div>
  <div class="form-group">
    <%= f.label :last_name %>

请帮忙。我不知道我哪里出错了。我正在upskillcourses.com上学习课程,然后我跟着视频,我甚至复制了代码并将其粘贴到文件中,以防我手动输入的前两次不正确。

这是routes.rb文件:

Rails.application.routes.draw do
root to: 'pages#home'
devise_for :users, controllers: { registrations: 'users/registrations' }
resources :users do
   resource :profile 
end
get 'about', to: 'pages#about'
resources :contacts, only: [:create]
get 'contact-us', to: 'contacts#new', as: 'new_contact'

Schema.rb:

ActiveRecord::Schema.define(version: 20170314180155) do

  create_table "contacts", force: :cascade do |t|
    t.string   "name"
    t.string   "email"
    t.text     "comments"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "plans", force: :cascade do |t|
    t.string   "name"
    t.decimal  "price"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "profiles", force: :cascade do |t|
  end

  create_table "users", force: :cascade do |t|
    t.string   "email",                  default: "", null: false
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.datetime "created_at",                          null: false
    t.datetime "updated_at",                          null: false
    t.integer  "plan_id"
    t.string   "stripe_customer_token"
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
  end

end

1 个答案:

答案 0 :(得分:0)

您的Profile模型似乎没有first_name字段。由于您没有收到“迁移”错误,我只能假设您需要在单独的迁移中将这两个字段添加为在线课程中的任务...

根据OP的评论:

# Add missing columns to profiles table
rails generate migration AddFirstNameAndLastNameToProfile first_name last_name

# Run migration
rails db:migrate