删除/销毁和编辑路由

时间:2015-11-08 01:29:22

标签: ruby-on-rails ruby routing restful-url

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

2 个答案:

答案 0 :(得分: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...>将其缩小到只有几个,而你的回溯会将其缩小到一定程度。

答案 1 :(得分: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'