Katharina Schreiber
Katharina Schreiber

Reputation: 1371

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

My view somehow doesn't get the show action of the controller and somehow returns user_id nil. Basically I have a UsersController and a ProfilesController and I am showing in Users#Index all the Users with First Name and Last Name.

<% @users.each do |user| %>
<table style="width:50%">
<tr>
    <th><%= link_to 'Show user profile', user_profile_path(@profile) %> <%= user.first_name%>   <%= user.last_name%></th>

</tr>
</table>

<% end %>

What I wish, is when I click on "Show user profile" I will get redirected to this user's profile. In my profiles controller I defined show action as follows:

class ProfilesController < ApplicationController
  before_action :set_profile, only: [:show, :edit, :update, :destroy]

  def show
    @user = User.eager_load(:profile).find(params[:user_id])
    @profile = @user.profile
  end

  def new
    @user = current_user
    @profile = Profile.new
  end

  def edit
    @user = current_user
    @profile = @user.profile
  end

  def create
    @user = current_user
    @profile = @user.build_profile(profile_params)

    respond_to do |format|
      if @profile.save
        format.html { redirect_to user_profile_path(current_user.id), notice: 'Profile was successfully created.' }
        format.json { render :show, status: :created, location: @profile }
      else
        format.html { render :new, notice: 'Did not save' }
        format.json { render json: @profile.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    respond_to do |format|
      if @profile.update(profile_params)
        format.html { redirect_to user_profile_path(current_user.id), 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

  def destroy
    @profile.destroy
    respond_to do |format|
      format.html { redirect_to users_url, notice: 'Profile was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private

    def set_profile
      @profile = current_user.profile
    end

    def profile_params
      params.fetch(:profile, {}).permit(:about, :avatar)
    end

end

Also here in my user model you can see the relaionship:

class User < ApplicationRecord
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable,
         :omniauthable, omniauth_providers: %i[facebook]


    has_one :profile, dependent: :destroy
    after_create :create_profile
    accepts_nested_attributes_for :profile
    validates :first_name, presence: true
    validates :last_name, presence: true


    def self.from_omniauth(auth)
        where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
            user.email = auth.info.email
            user.password = Devise.friendly_token[0, 20]
            name = auth.info.name
            user.first_name = name.split(" ")[0]
            user.last_name = name.split(" ")[1] 
        end
    end
end

If I am thinking right, it should get the profile as a @user.profile and show the profile of this certain user. But I get an error, saying id is nil.

Request

Parameters:

None

I also tried just experimenting:

<%= link_to 'Show user profile', user_profile_path(@user) %>
<%= link_to 'Show user profile', user_profile_path(@user.profile) %>
<%= link_to 'Show user profile', user_profile_path(@user,@profile) %>
<%= link_to 'Show user profile', user_profile_path(@user,profile) %>

... but none worked. Thank you!

Upvotes: 0

Views: 86

Answers (1)

Rajdeep Singh
Rajdeep Singh

Reputation: 17834

You need to pass user_id in link, use this instead

<%= link_to 'Show user profile', user_profile_path(user, @profile) %> 

Since it's a nested route you need to pass both the objects/ids.

Hope that helps!

Upvotes: 1

Related Questions