【发布时间】:2015-11-08 01:29:21
【问题描述】:
我是 Rails 的新手,正在尝试为一个项目构建自己的应用程序。我在尝试创建删除和编辑时不断收到此错误。
Listings中的NameError#show
这是我的 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' %>
【问题讨论】:
标签: ruby-on-rails ruby routing restful-url