【问题标题】:when i try to click to a post it shows me this error: Couldn't find Post with 'id'=#<Post::ActiveRecord_Relation:0x6311eb0>当我尝试单击帖子时,它向我显示此错误:找不到带有 'id'=#<Post::ActiveRecord_Relation:0x6311eb0> 的帖子
【发布时间】:2015-08-01 02:20:04
【问题描述】:

当我尝试去帖子显示抓取 id 时,我得到一个错误

Rails.application.routes.draw 做 devise_for :用户

资源:帖子做 资源:cmets 结束

根'posts#index'

#this is the show view

.show
  %p= @post.content



  = link_to "edit", edit_post_path(@post)
  = link_to "delete", post_path(@post), method: :delete, data: {confirm:        "Are you sure?"}
  = link_to "home", root_path

显示控制器

def show
        @post = Post.find(params[:id])

    end

【问题讨论】:

  • 您要重新格式化代码
  • 请发布您点击的链接的代码
  • 你的模型是什么样的?
  • 这就是我点击的内容:%p= link_to post.content, post_path(@posts)
  • 这是帖子模型:class Post

标签: ruby-on-rails


【解决方案1】:

你能显示你链接的页面吗?似乎您正在传递查询结果(活动记录关系)而不是 ID 作为参数,从而导致此错误。一个原因可能是如果您在链接中使用 where 语句而不是 find 语句,即说

link_to 'Show', Post.where(id: 1)

而不是

link_to 'Show', Post.find(id: 1)

但我们无法给出具体的答案,除非我们能看到链接来自的页面,而且最好能看到更多错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-03
    • 1970-01-01
    相关资源
    最近更新 更多