Я полный новичок в Rails, как, например, и я пытаюсь построить страницу, чтобы добавить дополнительные данные профиля, как только пользователь входит вRails:. NoMethodError по созданию HAS_ONE ассоциации с Разрабатывают модели
Я использую Завещание для аутентификации, и это работает нормально. Я получаю эту ошибку, и я застрял здесь.
неопределенный метод `профили
Можете ли вы помочь?
Коды
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
модели/Профиль .rb
class Profile < ActiveRecord::Base
belongs_to :user
end
Можете ли вы опубликовать полную ошибку? Кроме того, можете ли вы разместить свою модель пользователя? –
Hey Anthony, Я только что понял! Отношение: * has_one *. Итак, это должно быть '@profile = current_user.build_profile' вместо' @ profile = current_user.profiles.build' –