找不到具有id =的Object

时间:2015-06-24 13:02:08

标签: ruby-on-rails-4 rails-activerecord

我正在尝试向表中添加一列,该表显示我的活动记录中的所有产品,这些产品将允许我删除给定行的产品。当我尝试运行代码时,我得到一个与标题匹配的错误。

产品控制器

def destroy
@product = Product.find(params[:id])
@product.destroy
redirect_to :back
end

private

def product_params
  params.require(:product).permit(:ta_code, :tatr, :oem, :name, :kind, :ta_type, :id)
end

删除代码的按钮部分

<% Product.all.each do |product| %>
      <% if product.kind == "oem" %>
      <tr data-toggle="collapse" data-target="#all<%= product.tatr %>" class="accordion-toggle">
        <% if can? :destroy, Product %> 
          <td><%= button_to "delete", delete_path(@product), :method => :delete %></td>
        <% end %> 
        <td><%= product.id %></td>
        <td><%= product.tatr %></td>
        <td><%= product.name %></td>
        <td><%= product.oem %></td>
        <td><%= product.created_at.strftime("%b/%d/%y") %></td>
      </tr>
      <% end %>

的routes.rb

get 'signup' => 'users#new'
root 'static#home'
get 'systems' => 'static#pages'
get 'help' => 'static#help'
get 'ce' => 'static#ce'
get 'oem' => 'static#oem'
get 'update_existing' => 'static#update_existing'
get 'reports' => 'static#reports'
get 'miscellaneous' => 'static#miscellaneous'
get    'login'   => 'sessions#new'
post   'login'   => 'sessions#create'
delete 'logout'  => 'sessions#destroy'
get 'create_new' => 'products#new'
delete 'delete'=> 'products#destroy'
get 'new_status' => 'microposts#new'
get 'list' => 'users#list'
resources :users
resources :products
resources :microposts

这是我的第一个真正的项目,所以我确信会议有点过时,组织可能缺乏,但随着时间的推移。我感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

你在这里传递一个对象而不是它的id:

<% if can? :destroy, Product %>
   <td><%= button_to "delete", delete_path(:id => product), :method => :delete %></td>
<% end %>

应为delete_path(product)delete_path(id: product.id)

答案 1 :(得分:0)

替换

delete_path(@product)

delete_path(product)