2017-09-08 105 views
1

我已经从网站上抓取产品并将其插入我的数据库。所有的产品都在我的View页面上正确列出,但我似乎无法得到删除按钮的工作。有在我耙路线重复的原因是,我最初写手动路线了,但再使用按钮删除单个数据不起作用。

resources :ibotta 

我只是试图移动“资源:ibotta”的路线上,但没有工作。当我点击“消灭”按钮,它需要我的链接是

https://rails-tutorial2-chriscma.c9users.io/ibotta.14738

任何帮助非常感谢,谢谢。

查看

<h1>Show Page for iBotta</h1> 

<h3><%= @products.length %> products in the iBotta DB</h3> 

<% @products.each do |x| %> 
    <p>Title: <a href=<%=x.link%>><%= x.title %></a> </p> 
    <p>Value: <%= x.values %> </p> 
    <p>Store: <%= x.store %> </p> 

    <%= link_to 'Destroy', ibotta_path(x.id), 
       method: :delete %> 

<% end %> 

在控制器方法

def destroy 
    Ibotta.find(params[:id]).destroy 
    redirect_to ibotta_path 
end 

耙路线

   ibotta_save GET /ibotta/save(.:format)     ibotta#save 
       ibotta_show GET /ibotta/show(.:format)     ibotta#show 
      ibotta_delete GET /ibotta/delete(.:format)     ibotta#delete 

        ibotta GET /ibotta(.:format)      ibotta#index 
          POST /ibotta(.:format)      ibotta#create 
       new_ibottum GET /ibotta/new(.:format)     ibotta#new 
      edit_ibottum GET /ibotta/:id/edit(.:format)    ibotta#edit 
        ibottum GET /ibotta/:id(.:format)     ibotta#show 
          PATCH /ibotta/:id(.:format)     ibotta#update 
          PUT /ibotta/:id(.:format)     ibotta#update 
          DELETE /ibotta/:id(.:format)     ibotta#destroy 

回答

1

尝试只通过你的objec T和使用删除方法,使一个DELETE请求,如:

<% @products.each do |product| %> 
    <p>Title: <%= link_to product.title, product.link %></p> 
    <p>Value: <%= product.values %></p> 
    <p>Store: <%= product.store %></p> 

    <%= link_to 'Destroy', product, method: :delete %> 
<% end %> 

在创建a标签的情况下,可以使用link_to Rails的帮手。


正如我在你的项目中看到的,你没有一个layouts/application.html.erb文件,这就是为什么你呈现的一切不被yield在这个文件中传递,而你不添加没有在你的应用程序。 js或css文件,因此,您没有jQuery或jQuery UJS。这使得每次你点击锚标签来删除这个元素,执行一个GET请求,无论你是否指定要使用的方法。

这可以通过添加文件夹和文件,与任何项目,与最初的结构来解决:

<!DOCTYPE html> 
<html> 
<head> 
    <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %> 
    <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %> 
    <%= csrf_meta_tags %> 
</head> 
<body> 
    <%= yield %> 
</body> 
</html> 

注意,如果你这样做,你需要注释你javascripts/ibotta.coffee内容,否则你会得到错误:

SyntaxError: [stdin]:6:8: unexpected @

而我不知道为什么。

或者,如果你愿意继续没有此文件(我不推荐),您可以轻松地只是改变你的link_to帮手,为button_to帮手,如:

<%= button_to 'Destroy', x, method: :delete %> 

什么就给土特产品不同的HTML结构,但工程上删除记录:

<form class="button_to" method="post" action="/ibotta/id"> 
    <input type="hidden" name="_method" value="delete"> 
    <input type="submit" value="Destroy"> 
    <input type="hidden" name="authenticity_token" value="token"> 
</form> 

注意你有破坏和删除你的控制器的方法,我想你需要的是destroy,这是由您的resources糅工商业污水附加费。

Here是您可以看到它工作的存储库。

+0

我调整了一点这个例子,使它更具表现力,但是你在这里使用了'@products.each do | x |',所以你可以做'<%= link_to'Destroy', x,方法::删除%>。 –