Rails:与设计模型创建has_one关联时的NoMethodError

时间:2016-04-25 19:51:16

标签: ruby-on-rails ruby devise

我是Rails的完全初学者,我正在尝试构建一个页面,以便在用户登录后添加额外的个人资料数据。

我正在使用Devise进行身份验证,并且工作正常。我收到这个错误,我被困在这里。

  

未定义的方法`个人资料'

你能帮忙吗?

profiles_controller.rb

class ProfilesController < ApplicationController

  before_action :authenticate_user!, only: [:new, :create, :show]

  def new
    @profile = current_user.profiles.build
  end

  def create
    @profile = current_user.profiles.build(profile_params)
    if @profile.save
      format.html {redirect_to @profile, notice: 'Post was successfully created.'}
    else
      format.html {render 'new'}
    end

  end

  def show
    @profile = current_user.profiles
  end

  private

  def profile_params
    params.require(:profile).permit(:content)
  end
end

错误似乎来自这些行,特别是

  def new
    @profile = current_user.profiles.build
  end

其他参考编码:

/views/profiles/new.html.erb

<h1>Profiles#new</h1>
<p>Find me in app/views/profiles/new.html.erb</p>

<h3>Welcome <%= current_user.email %></h3>

<%= form_for(@profile) do |f| %>

  <div class="field">
    <%= f.label :content %><br />
    <%= f.text_field :text, autofocus: true %>
  </div>

  <div class="actions">
    <%= f.submit "Sign up" %>
  </div>
<%end%>

的routes.rb

Rails.application.routes.draw do
  get 'profiles/new'

  get 'profiles/create'

  get 'profiles/show'

  get 'profiles/update'

  get 'pages/home'

  get 'pages/dashboard'

  devise_for :users,  controllers: { registrations: "registrations" }
  resources :profiles


  root 'pages#home'

  devise_scope :user do
    get "user_root", to: "page#dashboard"
  end
end

模型/ user.rb

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

  has_one :profile, dependent: :destroy
end

模型/ profile.rb

class Profile < ActiveRecord::Base

  belongs_to :user
end

3 个答案:

答案 0 :(得分:2)

我刚想通了!

由于关系是has_one,我们应该使用

> model<-J48(Species ~ ., data = iris_dataset[[1]], control = Weka_control(M = 30, C = 0.05))
> model
J48 pruned tree
------------------

Petal.Width <= 0.6: setosa (46.0)
Petal.Width > 0.6
|   Petal.Width <= 1.7: versicolor (54.0/5.0)
|   Petal.Width > 1.7: virginica (36.0/1.0)

Number of Leaves  :     3

Size of the tree :  5

而不是

def new
   @profile = current_user.build_profile
end

根据文件 -
http://guides.rubyonrails.org/association_basics.html#has-one-association-reference

答案 1 :(得分:1)

您正试图调用未定义的关系:

foo

你应该打电话:

  def new
    @profile = current_user.profiles.build
  end

  has_one :profile

答案 2 :(得分:1)

1)如果您的用户必须拥有多个个人资料。在你的app / models / user.rb has_many :profiles

中设置

2)在您的ProfilesController中使用新方法代替@profile = current_user.profiles使用@profile = Profile.new

3)在您的routes.rb中删除

  get 'profiles/new'

  get 'profiles/create'

  get 'profiles/show'

  get 'profiles/update'

因为您已使用resources :profiles

4)要遵守DRY规则,您可以从部分渲染表单。只需在new.html.erb中添加相同内容的views / profiles / _form.html.erb,然后删除im.htm.erb中的所有内容并粘贴<%= render "form" %>。将来它会帮助您根据需要渲染编辑表单。

5)在您的ProfilesController中,您可以添加所有配置文件的方法索引

def index
  @profiles = Profile.all
end