我是Rails的新手,我正嘗試爲一個項目構建自己的應用程序。我在試圖創建刪除和編輯時不斷收到此錯誤。在房源#刪除/銷燬和編輯路由
NameError顯示
這是我的routes.rb文件...
Rails.application.routes.draw do
resources :listings
root 'listings#home'
get '/listings/new' => 'listings#new'
post '/listings' => 'listings#create'
get '/listings/:id' => 'listings#show'
get '/listings/:id/edit' => 'listings#edit'
patch '/listings/:id' => 'listings#update'
delete '/listings/:id' => 'listings#destroy'
end
# Prefix Verb URI Pattern Controller#Action
# listings GET /listings(.:format) listings#index
# POST /listings(.:format) listings#create
# new_listing GET /listings/new(.:format) listings#new
# edit_listing GET /listings/:id/edit(.:format) listings#edit
# listing GET /listings/:id(.:format) listings#show
# PATCH /listings/:id(.:format) listings#update
# PUT /listings/:id(.:format) listings#update
# DELETE /listings/:id(.:format) listings#destroy
這裏是我的房源控制器...
class ListingsController < ApplicationController
before_action :set_listing, only: [:edit, :update, :destroy]
def index
@listings = Listing.all
end
def show
@listings = Listing.find(params[:id])
end
def new
@listings = Listing.new
end
def create
@listings = Listing.new(listing_params)
@listings.save
redirect_to listings_path
end
end
def edit
end
def update
if @listings.update(listing_params)
redirect_to listings_path
else
render :edit
end
def destroy
@listings = Listing.find(params[:id])
redirect_to listings_url
end
def set_listing
@listings = Listing.find(params[:id])
end
def listing_params
params.require(:listing).permit(:address, :unit, :price, :description, :img_url)
end
end
這裏是我的show.html.erb文件...
<center><h2>Current Listing</h2></center>
<h4>Address:</h4> <h3><%= @listings.address %></h3>
<h4>Unit #:</h4> <h3><%= @listings.unit %></h3>
<h4>Price: $</h4> <h3><%= @listings.price %></h3>
<h4>Description:</h4> <h3><%= @listings.description %></h3>
<h4>Agent ID:</h4> <h3><%= @listings.agent_id %></h3>
<h4>Image:</h4> <h3><%= @listings.img_url %><p></h3>
<%= link_to 'Back', listings_path %>
<%= link_to 'Delete', listing, :method => 'delete' %>