Rails__Ассоциация
У меня 2 модели:
Модель пользователя и модель профиля.
Моя ассоциация выглядит следующим образом:
Class User < ActiveRecord::Base
has_one :profile
end
Class Profile < ActiveRecord::Base
belongs_to :user
validates user_id, presence: true
end
Мои контроллеры выглядит следующим образом:
USER:
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
if logged_in?
@micropost = current_user.microposts.build
@profile = current_user.build_profile
@new_comment = Comment.build_from(@user, current_user.id, ' ')
end
@microposts = @user.microposts.paginate(page: params[:page])
@profile_player = @user.profile
end
end
ПРОФИЛЬ:
class ProfilesController < ApplicationController
before_action :logged_in_user, only: [:create, :destroy]
before_action :correct_user, only: [:destroy]
def create
@profile = current_user.build_profile(profile_params)
if @profile.save
flash[:success] = 'Profile Has Been Published'
# redirect_to request.referrer || root_url
redirect_to users_url
else
render :'pages/home'
end
end
def update
@profile.update(profile_params)
redirect_to user_url
end
def destroy
end
private
def profile_params
params.require(:profile).permit(:name, :age, :nationality, :country, :city, :height, :weight,
:dominant_hand, :play_position, :highschool, :college, :team,
:awards, :highlights)
end
def correct_user
@profile = current_user.profile.find_by(id: params[:id])
redirect_to root_url if @profile.nil?
end
end
Теперь, что я пытаюсь сделать, это сделать частичное представление профиля на странице показа пользователя (следуя инструкции Майкла Хартла):
следовательно, я отображаю представление через переменную экземпляра, которую я создал в действии show Controller Users для профиля:
def show
#@username = params[:id] ==============> this is intended to show the user name instead
# of the user id in the address bar
@user = User.find(params[:id])
if logged_in?
@micropost = current_user.microposts.build
@profile = current_user.build_profile
@new_comment = Comment.build_from(@user, current_user.id, ' ')
end
@microposts = @user.microposts.paginate(page: params[:page])
@profile_player = @user.profile
end
Итак, на моей странице показа пользователя:
Я отображаю вид профиля следующим образом:
Теперь вот ошибка, с которой я сталкиваюсь, мой профиль сохраняется правильно при отправке формы, однако, когда я возвращаюсь на страницу отображения пользователя (я отображаю форму профиля на домашней странице пользователя), чтобы просмотреть профиль, я получаю Ошибка:
'nil' is not an ActiveModel-compatible object. It must implement :to_partial_path.
<div class="current-user-persona">
<%= render @profile_player %> =====> highlighted section for error
</div>
Я не уверен, что я делаю не так, вы можете мне помочь?
смотрел на это в течение нескольких дней.