简体   繁体   English

找不到带有“ id” =的帖子

[英]Couldn't find Post with 'id'=

I'm trying to link to a page that will show top responses for each post, however when I set everything up and click on the link_to button I get the error message: 我正在尝试链接到一个页面,该页面将显示每个帖子的热门回复,但是当我设置所有内容并单击link_to按钮时,我得到了错误消息:

ActiveRecord::RecordNotFound (Couldn't find Post with 'id'=): ActiveRecord :: RecordNotFound(找不到带有“ id” =的帖子):

I have this in my views: 我认为:

  <% if @post.user == current_user && @post.responses.where(top: true).size >= 1 %>
      <a href="#" class="btt"><%= link_to "finalize top", '/top' %>Finalize Top</a>

And I have my /top route set to show the top responses page: 我将/ top路由设置为显示顶部响应页面:

get '/top' => 'responses#top'

I understand that Rails doesn't know which post I'm referring to, but I'm not sure what needs to change in the views, routes, or responses controller. 我知道Rails不知道我要指的是哪个帖子,但是我不确定在视图,路线或响应控制器中需要更改哪些内容。

I also have this in the responses controller which I thought would help but didn't: 我在响应控制器中也有此功能,我认为这会有所帮助,但没有做到:

def top
  @post = Post.find(params[:post_id])
end

Any help is highly appreciated! 任何帮助深表感谢! This is my first time posting and am completely new to Ruby on Rails and programming. 这是我的第一次发布,对于Ruby on Rails和编程来说是全新的。

Here is the full error from Terminal: 这是终端的完整错误:

Started GET "/top" for ::1 at 2017-01-21 21:17:03 -0500
Processing by ResponsesController#top as HTML
  Post Load (0.1ms)  SELECT  "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1  [["id", nil]]
Completed 404 Not Found in 2ms (ActiveRecord: 0.1ms)

ActiveRecord::RecordNotFound (Couldn't find Post with 'id'=):
  app/controllers/responses_controller.rb:46:in `set_post'

I would try: 我会尝试:

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

First link_to is similar to <a> so in your view, you should putting link_to inside an <a> is like putting an <a> inside another <a> . 首先link_to类似于<a>在你看来是这样,你应该把link_to<a>就像是把一个<a>另一个内部<a> In your view put: 您认为:

<% if @post.user == current_user && @post.responses.where(top: true).size >= 1 %>
      <%= link_to "finalize top", { controller: :responses, action: :top, id: @post.id },{ class: 'btn btn-default'} %>

In your controller: 在您的控制器中:

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

In routes.rb 在routes.rb中

get '/top' => 'responses#top'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM