私は個人的なプロフィールを持つユーザーのイベントアプリを構築しています。私はサイトのユーザーをいくつか設定しましたが、各ユーザーのプロファイルを作成および/または編集しようとすると、フラッシュメッセージ「プロフィールはあなたのものではありません!これは設定され、うまく動作する私の最初のユーザープロファイルを参照しています。Ruby on Rails - ユーザーごとに新しいプロフィールを作成できません
私はDevise gemを初期設定に使用していますが、私自身のユーザコントローラを使ってそれらからビルドしています。このコントローラのコードは次のとおりです。 -
class UsersController < ApplicationController
before_action :authenticate_user!
before_action :set_user
before_action :owned_profile, only: [:edit, :update]
def new
@user = User.new
end
def show
@user = User.find(params[:id])
end
def create
end
def edit
@user = current_user #User.find_by(params[:id])
end
def update
@user = User.find_by(params[:id])
if @user.update(user_params)
redirect_to user_path, notice: "Profile successfully updated!"
else
render 'edit'
end
end
private
def user_params
params.require(:user).
permit(:name, :username, :biography, :email, :url)
end
def owned_profile
unless current_user == @user
flash[:alert] = "That profile doesn't belong to you!"
redirect_to root_path
end
end
def set_user
@user = User.find_by(params[:id])
end
end
ご協力いただければ幸いです。
お返事ありがとうございますが、最初の問題が解決されているかどうかはわかりませんが、ユーザーごとに自分のプロフィールを編集したいと思っています。現在のところ、フラッシュメッセージに戻ってきます。 –
それを解決します。彼らはまだプロファイルを編集することができ、そうすることもできます。 – toddmetheny
サイトの管理者として自分自身を設定したくない場合はどうすればいいですか? –