缺少必需的密钥:[:id]

时间:2018-03-07 14:19:11

标签: ruby-on-rails ruby

我尝试链接到导航部分中的用户个人资料。

<% 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 %>

例如,当我尝试导航到个人资料/新时 它给了我错误:没有路由匹配{:action =&gt;&#34; show&#34;,:controller =&gt;&#34; profiles&#34;,:id =&gt; nil}缺少必需键:[:id]

但是在我的条件下,当用户还没有个人资料时,它甚至应该运行代码。 条件本身有问题吗?我错过了什么?

个人资料控制器

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

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找不到工作的解决方案,提前谢谢!

0 个答案:

没有答案