Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/55.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 不能';无法使用';id';_Ruby On Rails - Fatal编程技术网

Ruby on rails 不能';无法使用';id';

Ruby on rails 不能';无法使用';id';,ruby-on-rails,Ruby On Rails,当我使用“销毁”方法时,向我显示一个错误 Couldn't find Post with 'id'=#<Post::ActiveRecord_Relation:0x000000045e14c0> 看法 很抱歉我的英语不好您有定义为ActiveRecord::Relation的@posts变量(某种类型的模型数组,定义为@posts=Post.all或类似的东西)。为了解决您的问题,如果您要在索引url内调用link_,很可能您正在执行以下操作: <% @posts.eac

当我使用“销毁”方法时,向我显示一个错误

Couldn't find Post with 'id'=#<Post::ActiveRecord_Relation:0x000000045e14c0>
看法



很抱歉我的英语不好

您有定义为ActiveRecord::Relation的
@posts
变量(某种类型的模型数组,定义为
@posts=Post.all
或类似的东西)。为了解决您的问题,如果您要在索引url内调用link_,很可能您正在执行以下操作:

<% @posts.each do |post| %>
  # link_to call somewhere here
<% end %>

#链接到这里的某个地方
然后,您必须更改要呼叫的链接

<%= link_to 'Destroy', post_path(post),
          method: :delete,
          data: { confirm: 'Are you sure?' } %>


请注意,我们使用中定义的变量。每个do | var |块,这里是
post

如何创建
@posts
?您不应该将特定的post传递给link|to方法吗?@vveare138将该信息放入问题中是完全可以接受的,并且更可取(您可以编辑自己的post来添加信息)。但是,这并没有告诉我在哪里创建了
@posts
。pdoherty926所说的可能是正确的,但是我们需要更多的信息来确定。对不起,现在更新一篇帖子
<% @posts.each do |post| %>
  # link_to call somewhere here
<% end %>
<%= link_to 'Destroy', post_path(post),
          method: :delete,
          data: { confirm: 'Are you sure?' } %>