我昨天提交了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。正如上面提到的鏈接問題,我是一個完整的新手,所以請原諒我,如果這是一個明顯的修復。
你想什麼用戶要得到?登錄的車輛或車主?汽車的主人 – Iceman
。我試圖鏈接到發佈該車的用戶。 –
你可以用'@ car.user'得到它 – Iceman