0

I try to link to a Users profile within my navigation partial.

<% if user_signed_in?  %>
      concat <li><%= link_to 'Neues Jobangebot einstellen', new_job_path %>
      </li>
      <% if current_user.profile.blank? %>
        <li><%= link_to 'New Profile' , new_profile_path %></li>
        <% else %>
        <li><%= link_to 'My Profile', profile_path(current_user.profile) %>
      </li>
      <% end %>

        <li><%= link_to 'Logout', destroy_user_session_path, method: :delete 
      %></li>
      <% else %>
        <li><%= link_to 'Login', new_user_session_path %>
        <li><%= link_to 'Register', new_user_registration_path %>
      <% end %>

For example, when I try to navigate to profiles/new It gives me the error: No route matches {:action=>"show", :controller=>"profiles", :id=>nil} missing required keys: [:id] .

But in my conditional it should't even run the code when the User has no profile yet. Is something wrong with the conditional itself? What am I missing?

Profiles Controller

class ProfilesController < ApplicationController
before_action :set_profile, only: [:show, :edit, :update, :destroy]
access all: [:show, :index], user: :all, site_admin: :all


def index
    @profiles = Profile.all.page(params[:page]).per(10)
end

def new
  if current_user.profile.blank?
    @profile = current_user.build_profile
  else
    redirect_to root_path
    flash[:notice] = "Du besitzt bereits ein Profil"
  end
end

def create
    @profile = current_user.build_profile(profile_params)
    if @profile.save
        redirect_to @profile
    else
        flash[:notice] = "Das Profil konnte nicht erstellt werden, bitte 
        fülle alle Felder aus!"
        render 'new'
    end
end

def show
end

def edit
end

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

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

def profile_params
    params.require(:profile).permit(:Titel, :Beschreibung, :Name, 
:profileavatar)
end

def set_profile
    @profile = Profile.find(params[:id])
end

end

My routes are **default resources** 

Rails.application.routes.draw do

  devise_for :users
  root 'jobs#index'

  resources :jobs
  resources :profiles


end

raked routes

                root GET    /                              jobs#index
                jobs GET    /jobs(.:format)                jobs#index
                     POST   /jobs(.:format)                jobs#create
             new_job GET    /jobs/new(.:format)            jobs#new
            edit_job GET    /jobs/:id/edit(.:format)       jobs#edit
                 job GET    /jobs/:id(.:format)            jobs#show
                     PATCH  /jobs/:id(.:format)            jobs#update
                     PUT    /jobs/:id(.:format)            jobs#update
                     DELETE /jobs/:id(.:format)            jobs#destroy
            profiles GET    /profiles(.:format)            profiles#index
                     POST   /profiles(.:format)            profiles#create
         new_profile GET    /profiles/new(.:format)        profiles#new
        edit_profile GET    /profiles/:id/edit(.:format)   profiles#edit
             profile GET    /profiles/:id(.:format)        profiles#show
                     PATCH  /profiles/:id(.:format)        profiles#update
                     PUT    /profiles/:id(.:format)        profiles#update
                     DELETE /profiles/:id(.:format)        profiles#destroy

Haven't found a working solution, thanks in advance!

  • Your routes are messed up if it's using the "show" action on the profiles/new url. Post your routes please – bkunzi01 Mar 07 '18 at 14:31
  • Rails.application.routes.draw do devise_for :users root 'jobs#index' resources :jobs resources :profiles end – Mark Billes Mar 07 '18 at 14:46
  • What's the output when you run `rake routes` in the terminal? – Yechiel K Mar 07 '18 at 14:56
  • new_profile GET /profiles/new(.:format) profiles#new – Mark Billes Mar 07 '18 at 15:01
  • `profile_path(current_user.profile)` <- What is `current_user.profile`? Is this an id or a string? I've seen this error before when you use something like `profile/mark` instead of `profile/1`. If you have something like friendly_id gem, it assumes the string is an id, and you need to update your routes. – gwalshington Mar 07 '18 at 15:02
  • I dont use friendly_id. I assume it's an id? – Mark Billes Mar 07 '18 at 15:04
  • run `rake routes` and add the output to the question itself. Although the routes posted look fine there is a ordering error in the routes if `/profiles/new` is being matched to the show action. – max Mar 07 '18 at 15:10
  • Also don't use blank here - its for testing user input strings. You want to use `if current_user.profile` and flip the order of the if and else. – max Mar 07 '18 at 15:13
  • I tried it, still not working @max – Mark Billes Mar 07 '18 at 15:14
  • Thats puzzling. It looks completely fine. I would try isolating the problem. If getting `/profiles/new` in a browser gives you this issue you can remove the view from the equation completely. https://stackoverflow.com/help/mcve – max Mar 07 '18 at 15:33

0 Answers0