LoginSignup
1
0

More than 3 years have passed since last update.

Profile編集/

Last updated at Posted at 2020-11-17

ProfileのMVCを作成する

rails g scaffold profiles name:string birthday:date user_id:integer

ログイン画面からprofileの作成のアクション

下記のコードをcommentsのindexファイルに追加する

<%= link_to 'New Profile', new_profile_path %>

親関係整える

1Userにつき1Profile

#追加 とかいているところを追加する

class ProfilesController < ApplicationController
  before_action :set_profile, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_user! #追加
  # GET /profiles
  # GET /profiles.json
  def index
    @profiles = Profile.all
  # GET /profiles/1
  # GET /profiles/1.json
  def show
  end
  # GET /profiles/new
  def new
    @profile = Profile.find_or_create_by(:user_id => current_user.id)
    redirect_to edit_profile_url(@profile)#追加
  end
  # GET /profiles/1/edit
  def edit
    if @profile.user_id != current_user.id
      raise ActionController::RoutingError.new("Not authorized")
    end #追加
  end
  # POST /profiles
  # POST /profiles.json
  def create
    @profile = Profile.new(profile_params)
    # @profile.save!
    # 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(params[:id])
  end
  # Only allow a list of trusted parameters through.
  def profile_params
    params.require(:profile).permit(:name, :birthday, :user_id)
  end
end
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0