2017-02-13 65 views
0

我昨天提交了this question,并且大部分能够从我到达那里的答案中解决问题,但是我现在有一个不同但相关的问题。“找不到用户'id'=”

我在我的节目鉴于这种代码为我的对象,租车:

  <%= link_to @user do %>    
       <%= image_tag @user.profile.avatar.url, class: 'user-index-avatar' %>    
      <h3><%= @user.profile.company %></h3> 
      <% end %> 
      <h4><%= @user.profile.location %></h4> 
      <h4><%= @user.profile.phone_number %></h4> 
      <h4><%= @user.profile.email %></h4> 
      <% if user_signed_in? && @user.id == current_user.id %> 
      <%= link_to "Edit Listing", edit_car_path(id: @car.id), class: 'btn btn-lg btn-warning btn-block' %> 
      <% end %> 

当我去这个页面,我得到错误信息“的ActiveRecord :: RecordNotFound在CarsController#秀”和“Couldn “T找到与用户的 'id'=”

耙路线:

   cars GET /cars(.:format)      cars#index 
        GET /cars(.:format)      cars#index 
        POST /cars(.:format)      cars#create 
      new_car GET /cars/new(.:format)     cars#new 
      edit_car GET /cars/:id/edit(.:format)    cars#edit 
       car GET /cars/:id(.:format)     cars#show 
        PATCH /cars/:id(.:format)     cars#update 
        PUT /cars/:id(.:format)     cars#update 
        DELETE /cars/:id(.:format)     cars#destroy 
    new_user_profile GET /users/:user_id/profile/new(.:format) profiles#new 
    edit_user_profile GET /users/:user_id/profile/edit(.:format) profiles#edit 
     user_profile GET /users/:user_id/profile(.:format)  profiles#show 
        PATCH /users/:user_id/profile(.:format)  profiles#update 
        PUT /users/:user_id/profile(.:format)  profiles#update 
        DELETE /users/:user_id/profile(.:format)  profiles#destroy 
        POST /users/:user_id/profile(.:format)  profiles#create 
      user_cars GET /users/:user_id/cars(.:format)   cars#index 
        POST /users/:user_id/cars(.:format)   cars#create 
     new_user_car GET /users/:user_id/cars/new(.:format)  cars#new 
        GET /cars/:id/edit(.:format)    cars#edit 
        GET /cars/:id(.:format)     cars#show 
        PATCH /cars/:id(.:format)     cars#update 
        PUT /cars/:id(.:format)     cars#update 
        DELETE /cars/:id(.:format)     cars#destroy 

型号:

class Car < ActiveRecord::Base 
belongs_to :user 
end 

class User < ApplicationRecord 
belongs_to :plan 
has_one :profile 
has_many :cars 

的routes.rb

get 'cars' => 'cars#index', as: :cars 
resources :cars 
resources :users do 
    resource :profile 
    resources :cars, shallow: true 
end 

CarsController:

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

误差指定问题是与 “@user = User.find(PARAMS [:USER_ID])”,但我不知道如何定义@user。正如上面提到的链接问题,我是一个完整的新手,所以请原谅我,如果这是一个明显的修复。

+0

你想什么用户要得到?登录的车辆或车主?汽车的主人 – Iceman

+0

。我试图链接到发布该车的用户。 –

+1

你可以用'@ car.user'得到它 – Iceman

回答

0

Rails find方法在无法通过指定的id查找记录时引发此异常。您可以在params中仔细检查您的user_id,并查看该ID的记录是否确实存在于数据库中。

并且由于您粘贴的错误,您的参数中似乎user_id为空。 你总是可以做一些事情像

puts "Look for me in console\n"*10 
puts params.inspect 

,比日志窗口什么PARAMS您实际接收见。

我看到你的问题是在评论由汽车模型访问用户协会解决,但是这个答案是为了指出问题的根源,减少悬而未决的问题的数量SO :)

+0

非常感谢:-) –