Pepeng Agimat
Pepeng Agimat

Reputation: 167

No route matches {:action=>"show", :controller=>"user_profiles", :id=>nil, :user_id=>"2"} missing required keys: [:id]

I got an error in my routes when I want to redirect to show action after the user_profile created.

Controller

class UserProfilesController < ApplicationController

    def create
        @user = User.find(current_user.id) 
        @profile = UserProfile.find_by_user_id(@user)

        if @user_profile = @user.create_user_profile(user_params)
            redirect_to user_user_profile_url(@user,@profile)
        end
    end

    def show
        @user_profile = @user.user_profile
    end

    private

    def user_params
        params.require(:user_profile).permit(:firstname, :lastname, :birthdate, :street_address, :city_address, :country, :contact_number, :avatar)
    end
end

routes.rb

 resources :users do
    resources :user_profiles
  end

Thank you!

Upvotes: 0

Views: 1994

Answers (1)

dimuch
dimuch

Reputation: 12818

The @profile instance variable most likely is nil, since you are going to create a new profile, not update existing one. Try to redirect to newly created profile instead.

def create
    @user = User.find(current_user.id) 
    @profile = UserProfile.find_by_user_id(@user)

    if @user_profile = @user.create_user_profile(user_params)
        redirect_to user_user_profile_url(@user,@user_profile)
    end
end

Upvotes: 2

Related Questions