【发布时间】:2013-03-12 14:47:38
【问题描述】:
路线
resources :links, only: [:new, :create, :index, :destroy]
links_controller
class LinksController < ApplicationController
before_filter :signed_in_user
def new
@user = current_user
@tags = @user.tags
@link = Link.new
end
def create
@link = Link.new(params[:link])
if @link.save
flash[:success] = "Link created!"
redirect_to root_url
else
render 'new'
end
end
def index
@links = Link.paginate(page: params[:page])
end
def destroy
@link.destroy
redirect_to links_path
end
end
html.erb
<li>
<span class="location_info"> From Tag: <%= link.from_tag.ref %></span>
<span class="location_info"> To Tag: <%= link.to_tag.ref %></span>
<span class="location_info"> Cost: <%= link.value %>
| <%= link_to "delete", link, method: :delete,
data: { confirm: "You sure?" } %></span>
</li>
为什么会出现这个错误?
【问题讨论】:
-
销毁操作需要
id,因此它可以搜索记录(在您的情况下,它需要一个 id 来搜索@link) -
您应该查看出现此错误的代码。它很可能在您尝试添加删除链接的视图中。
-
link很可能是一条新记录(它没有id),这就是您收到错误的原因。 -
删除链接是索引页面的一部分,所以它肯定有一个id!
-
在你的部分中做一个
<%= link.inspect -%>,这样我们就可以看到链接的实际样子。和一个rake routes | grep -i link只是为了确定......
标签: ruby-on-rails ruby-on-rails-3.2 routes