[英]Rails: NoMethodError on creating has_one association with devise model
I am a complete beginner in Rails as such and I am trying to build a page to add extra profile data once the user logs in. 我是Rails的完全入门者,我试图构建一个页面来在用户登录后添加额外的个人资料。
I am using Devise for authentication purposes and that works fine. 我使用Devise进行身份验证,效果很好。 I get this error and I have been stuck here.
我收到此错误,并且一直被卡在这里。
undefined method `profiles'
未定义的方法`profiles'
Can you please help? 你能帮忙吗?
Codes 代号
profiles_controller.rb 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
The error seems to be coming from these lines in particular 错误似乎特别来自这些行
def new
@profile = current_user.profiles.build
end
Other codes for reference: 其他代码供参考:
/views/profiles/new.html.erb /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 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
models/user.rb 型号/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
models/profile.rb 模型/ profile.rb
class Profile < ActiveRecord::Base
belongs_to :user
end
I just figured it out! 我只是想通了!
As the relationship is has_one, we should be using 由于关系为has_one,因此我们应该使用
def new
@profile = current_user.build_profile
end
instead of 代替
def new
@profile = current_user.profiles.build
end
according to the documentation - 根据文档-
http://guides.rubyonrails.org/association_basics.html#has-one-association-reference http://guides.rubyonrails.org/association_basics.html#has-one-association-reference
You are trying to call an undefined relationship: 您正在尝试调用未定义的关系:
def new
@profile = current_user.profiles.build
end
has_one :profile
You should be calling: 您应该打电话给:
def new
@profile = current_user.build_profile
end
1) If your user must have many profiles. 1)如果您的用户必须有许多配置文件。 Set in your app/models/user.rb
has_many :profiles
在您的app / models / user.rb中设置
has_many :profiles
2) In your ProfilesController in new method instead of @profile = current_user.profiles
use @profile = Profile.new
2)在您的ProfilesController中以新方法代替
@profile = current_user.profiles
使用@profile = Profile.new
3) In your routes.rb delete 3)在你的routes.rb中删除
get 'profiles/new'
get 'profiles/create'
get 'profiles/show'
get 'profiles/update'
because you have already used resources :profiles
因为您已经使用过
resources :profiles
4) To stay with rules of DRY you can render form from a partial. 4)为了遵守DRY规则,您可以从局部渲染表格。 Just add in views/profiles/_form.html.erb with the same content in your new.html.erb and after this you can delete everything im new.htm.erb and paste
<%= render "form" %>
. 只需在new.html.erb中添加具有相同内容的views / profiles / _form.html.erb,然后您就可以删除new.htm.erb中的所有内容并粘贴
<%= render "form" %>
。 In future it will help you to render edit form if you want. 将来,如果需要,它将帮助您呈现编辑表单。
5) In your ProfilesController you can add method index with all profiles 5)在您的ProfilesController中,您可以为所有配置文件添加方法索引
def index
@profiles = Profile.all
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.