继续获得未初始化的常量ProfileController

时间:2018-02-04 03:40:10

标签: ruby-on-rails ruby

当我尝试转到新配置文件的链接时,继续收到此错误消息。每个用户只有一个配置文件,配置文件属于用户。在网上找不到任何解决方案。好像我的所有文件和代码都写得正确。这是我的代码:

home.html.erb

<%= link_to "Create your profile!", new_user_profile_path(user_id: current_user.id) if user_signed_in? %>

简档/ _form.html.erb

<div class="mdl-grid">
  <div class=" mdl-cell mdl-cell--4-col"></div>
        <div class="mdl-card mdl-shadow--6dp mdl-cell mdl-cell--4-col">
            <div class="mdl-card__title mdl-color--primary mdl-color-text--white">
                <h2 class="mdl-card__title-text">Create your profile</h2>
            </div>
        <div class="mdl-card__supporting-text">
        <%= form_for @profile, url: user_profile_path do |f| %>
          <div class="mdl-textfield mdl-js-textfield field">
            <p>First name</p>
            <%= f.text_field :first_name, autofocus: true, class: 'mdl-textfield__input', placeholder: 'Elon' %>
          </div>
          <div class="mdl-textfield mdl-js-textfield field">
            <p>Last name</p>
            <%= f.text_field :last_name, autofocus: true, class: 'mdl-textfield__input', placeholder: 'Musk' %>
          </div>
          <div class="mdl-textfield mdl-js-textfield field">
            <p>Contact Email</p>
            <%= f.email_field :contact_email, autofocus: true, class: 'mdl-textfield__input', placeholder: 'elonmusk@boringcompany.com' %>
          </div>
          <div class="mdl-textfield mdl-js-textfield field">
            <p>Description</p>
            <%= f.text_area :description, autofocus: true, class: 'mdl-textfield__input', placeholder: 'I like self-driving cars, rocket ships, and solar energy! Oh and boring tunnels.' %>
          </div>
          <br>
          <div class="mdl-card__actions mdl-card--border actions">
            <%= f.submit "Update Profile", class: 'signupform mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect mdl-button--accent' %>
          </div>
          <% end %>
        </div>
            </div>
      <div class=" mdl-cell mdl-cell--4-col"></div>
    </div>

简档/ new.html.erb

<%= render 'form' %>

profile.rb

class Profile < ActiveRecord::Base
  belongs_to :user
end

user.rb

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
  has_one :profile

end

profiles_controller.rb

class ProfilesController < ApplicationController
  # GET to /users/:user_id/profile/new
  def new
    # Render blank profile details form
    @profile = Profile.new
  end
end

的routes.rb

  resources :users do
    resources :profile
  end

1 个答案:

答案 0 :(得分:0)

控制器名称是复数,因此您的路线应该是这样的

 resources :users do
    resources :profiles
  end

注意:profiles,而不是profile