I created the model User and the model Profile. On my homepage I have a link in the dropmenu navigation bar that links to Edit Profile. The problem I face is "No route matches {:action=>"edit", :controller=>"profiles", :id=>nil} missing required keys: [:id]".
The route for edit page is "edit_profile_path" with verb GET and URI pattern "/profiles/:id/edit(.:format)". I am having a hard time getting the "id" inserted. Below is the code that I have on my app.
In model Profile file I have:
class Profile < ActiveRecord::Base
belongs_to :user, dependent: :destroy
end
In model User file I have:
class User < ActiveRecord::Base
has_one :profile
end
The profile has many attributes, but one of them is "user_id" which is an integer that is equal to the User's id. So User #5 with id#5 is the owner of Profile#5. Here is the code that I have in the View file:
<li><%= link_to "Edit Profile", edit_profile_path(@profile) %></li>
With regards to the code directly above, I have tried inserting different codes inside the parenthesis, from @profile.id, @profile, @user.id, and @user. But it has not worked.
I created a profiles controller and I think (but I am not certain) that my problem is coming from the profiles_controller file. Here is the code that I have:
class ProfilesController < ApplicationController
before_action :authenticate_user!
before_action :set_profile, only: [:edit, :update]
def edit
end
def new
@profile = Profile.new
end
def create
@profile = Profile.new(profile_params)
@profile.user_id = current_user.id
if @profile.save
redirect_to welcome_path
else
render 'new'
end
end
def update
@profile.update(profile_params)
redirect_to welcome_path
end
private
def set_profile
@profile = Profile.find(params[:id])
end
end
You are getting this error because in your view, your @profile
in nil
.
So, you have to get the current_profile
in your view so that you can go to the edit page of that profile.
If you already have access to your current_user
helper method, then, in your view, you can simply do:
<li><%= link_to "Edit Profile", edit_profile_path(current_user.profile) %></li>