我使用设计和user
模型创建了profile
模型,一切正常。我试图为用户创建一个链接,以便能够转到他的个人资料并编辑个人资料
<li><%= link_to "Profile", edit_profile_path(current_user) %></li>
我收到错误:ActiveRecord::RecordNotFound in ProfilesController#show
用户has_one :profile
,所有关联都已正确关联,并且user_id
模型中的profile
列正在按预期保存数据。
它告诉我错误在这里:
def set_profile
@profile = Profile.find(params[:id])
end
我一直试图解决这个问题,但没有运气?
profile_controller.rb:
class ProfilesController < ApplicationController
before_action :set_profile, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, only: [:show, :new, :edit, :create, :update, :destroy]
# GET /profiles
# GET /profiles.json
def index
@profiles = Profile.all
end
# GET /profiles/1
# GET /profiles/1.json
def show
end
# GET /profiles/new
def new
@profile = current_user.build_profile
end
# GET /profiles/1/edit
def edit
end
# POST /profiles
# POST /profiles.json
def create
@profile = current_user.build_profile(profile_params)
respond_to do |format|
if @profile.save
format.html { redirect_to @profile, notice: 'Profile was successfully created.' }
format.json { render :show, status: :created, location: @profile }
else
format.html { render :new }
format.json { render json: @profile.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /profiles/1
# PATCH/PUT /profiles/1.json
def update
respond_to do |format|
if @profile.update(profile_params)
format.html { redirect_to @profile, notice: 'Profile was successfully updated.' }
format.json { render :show, status: :ok, location: @profile }
else
format.html { render :edit }
format.json { render json: @profile.errors, status: :unprocessable_entity }
end
end
end
# DELETE /profiles/1
# DELETE /profiles/1.json
def destroy
@profile.destroy
respond_to do |format|
format.html { redirect_to profiles_url, notice: 'Profile was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_profile
@profile = Profile.find_by(user_id: params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def profile_params
params.require(:profile).permit(:bio, :user_id, educations_attributes: [:id, :university, :course, :yearCompleted, :_destroy])
end
end
路线:
resources :profiles
devise_for :users, controllers: {registrations: "registrations"}
get 'home/index'
root 'home#index'
已更新
我改变了:
def set_profile
@profile = Profile.find(params[:id])
end
要
def set_profile
@profile = Profile.find_by(user_id: params[:id])
end
将我带到用户的正确编辑视图,但是当我点击更新时,它会抛出一个错误:未定义的方法`update&#39;为零:NilClass
答案 0 :(得分:1)
使用时
edit_profile_path(current_user)
params [:id]是current_users id。尝试像
这样的东西@profile = Profile.find_by(user_id: params[:id])
对用户个人资料使用单一资源可能更有意义。您可以在Rails指南中阅读有关单一资源的更多信息:http://guides.rubyonrails.org/routing.html#singular-resources
路由文件:
ressource :profile
简化控制器:
class ProfilesController < ApplicationController
before_action :authenticate_user!
before_action :load_profile
def show; end
def edit; end
def update
respond_to do |format|
if @profile.update(profile_params)
format.html { redirect_to @profile, notice: 'Profile was successfully updated.' }
format.json { render :show, status: :ok, location: @profile }
else
format.html { render :edit }
format.json { render json: @profile.errors, status: :unprocessable_entity }
end
end
end
private
def load_profile
@profile = current_user.profile || current_user.build_profile
end
def profile_params
params.require(:profile).permit(:bio, :user_id, educations_attributes: [:id, :university, :course, :yearCompleted, :_destroy])
end
end
编辑个人资料页面的示例链接:<li><%= link_to "Profile", edit_profile_path %></li>