2015-11-08 79 views
1

我是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' %> 

回答

1

錯誤是最有可能在這裏:

<%= link_to 'Delete', listing, :method => 'delete' %> 

listing沒有定義,你應該使用你在控制器中定義@listings

其他幾件事:

  1. @listings在這裏極其錯誤的名稱。如果是單一物體,請使用單數形式@listing。當你在明年回到這個代碼時,它會讓你的生活更輕鬆。當然,在處理集合(索引操作)時,仍然應該使用@listings

  2. 你已經有了一個方法來做你在做什麼show action - set_listings。只需將show添加到before_action旁邊的列表中即可幹掉您的代碼。

  3. resources :listings已爲所有列表操作創建路由,因此不需要「手動」定義它們。

  4. 始終,始終,始終查看(並在您的問題中發佈)完整的錯誤消息。它會給你更多的想法。有幾千個原因可以提高NameErrornameError: undefined method listing for <ActionViev...>將其縮小爲幾個,並且您的回溯將會進一步縮小到某個點。

0

更改刪除鏈接

<%= link_to 'Delete', listing_path(@listings), 
      :confirm => 'Are you sure?', :method => :delete %> 

刪除以下的文件航線從routes.rb中路由作爲resources :listings會自動提供這些路線

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'