Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 没有路由匹配{:action=>;>;destroy";,:controller=>;>;links";}_Ruby On Rails_Ruby On Rails 3.2_Routes - Fatal编程技术网

Ruby on rails 没有路由匹配{:action=>;>;destroy";,:controller=>;>;links";}

Ruby on rails 没有路由匹配{:action=>;>;destroy";,:controller=>;>;links";},ruby-on-rails,ruby-on-rails-3.2,routes,Ruby On Rails,Ruby On Rails 3.2,Routes,路线 resources :links, only: [:new, :create, :index, :destroy] 链接\u控制器 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

路线

   resources :links, only: [:new, :create, :index, :destroy]
链接\u控制器

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>
  • 从标签: 要标记: 费用: |
  • 为什么我会犯这个错误


    您在github上的代码与此处的代码不同,但是, 您不需要先在
    destroy
    操作中找到链接,然后再销毁它吗? 现在,它在一个空的
    @link
    变量上调用
    destroy

    由于链接和路由看起来不错,我猜您应该采取如下销毁操作:

    def destroy
      @link= Link.find(params[:id])
      @link.destroy
      redirect_to links_path
    end
    

    除非你在应用程序的某个地方有一个过滤器可以找到@link对象。在github上更新您的代码,以便我可以下载它并将其安装到我的计算机上,以便在上述解决方案不起作用时进行更多调查。

    销毁操作需要一个
    id
    ,以便它可以搜索记录(在您的情况下,它需要一个id来搜索@link)。您应该查看出现此错误的代码。它很可能在视图中尝试添加删除链接。
    链接
    很可能是一条新记录(它没有
    id
    ),这就是为什么会出现错误。删除链接位于索引页的部分,因此它肯定有id!在你的部分中执行
    ,这样我们就可以看到链接的实际外观。还有一个
    rake routes | grep-i链接,以确保。。。