【问题标题】:" Couldn't find User with 'id'= "" 找不到具有 'id'= 的用户"
【发布时间】:2017-02-13 23:22:55
【问题描述】:

我昨天提交了this question 并且大部分能够从我得到的答案中解决问题,但是我现在遇到了一个不同但相关的问题。

我的对象 Car 的显示视图中有此代码:

            <%= 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 in CarsController#show”和“找不到具有 '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

汽车控制器:

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

错误表明问题出在“@user = User.find(params[:user_id])”上,但我不确定如何定义@user。正如上面链接的问题中提到的,我是一个完整的新手,所以如果这是一个明显的解决方法,请原谅我。

【问题讨论】:

  • 你想获得什么用户?登录的人还是车主?
  • 车主。我正在尝试链接回发布汽车的用户。
  • 你可以通过@car.user得到它
  • 在我之前链接到的上一个问题中,当我使用嵌套路由时,相同的代码以前可以工作,但我现在不知道如何让它工作。
  • @car.user 工作 - 非常感谢!

标签: ruby-on-rails


【解决方案1】:

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

根据您粘贴的错误,您的参数中的 user_id 似乎为空。 你总是可以做一些事情,比如

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

然后在日志窗口中查看您实际收到的参数。

我看到你的问题是通过访问汽车模型中的用户关联在 cmets 中解决的,但这个答案旨在指出问题的根源并减少 SO 中未回答问题的数量:)

【讨论】:

    猜你喜欢
    • 2015-07-15
    • 1970-01-01
    • 2015-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-24
    • 2016-10-19
    • 2012-05-24
    相关资源
    最近更新 更多