ActiveRecord::RecordNotFound in PostsController#show [英] ActiveRecord::RecordNotFound in PostsController#show

查看:34
本文介绍了ActiveRecord::RecordNotFound in PostsController#show的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到上述错误当我访问 http://127.0.0.1:3000/posts/show url 时,它给了我错误

I am getting the above error When I accessing the http://127.0.0.1:3000/posts/show url its giving me the error

Couldn't find Post with id=show
Extracted source (around line #5) 

def show
@post = Post.find(params[:id])--here is line number 5
end
def create
@post = Post.new(post_params)

activerecord (4.0.3) lib/active_record/relation/finder_methods.rb:198:in `raise_record_not_found_exception!'

当我访问 url 时同样的问题 http://127.0.0.1:3000/posts/index...

same problem when I am accessing the url http://127.0.0.1:3000/posts/index...

my controller file is

posts_controller.rb

posts_controller.rb

class PostsController < ApplicationController
  def new
end
def show
  @post = Post.find(params[:id])
end
  def create
  @post = Post.new(post_params)

  @post.save
  redirect_to @post
  # or this command also works redirect_to action: :show, id: @post.id
end

def index
  @posts = Post.all
end
private
  def post_params
    params.require(:post).permit(:title, :text)
  end

end

my routes.rb file is
Blog::Application.routes.draw do
  resources :posts
  root to: "posts#index"
end

index.html.rb 文件是

index.html.rb file is

<h1>Listing posts</h1>
 <h3><%= link_to 'New post', new_post_path %></h3>
<table>
  <tr>
    <th>Title</th>
    <th>Text</th>
  </tr>

  <% @posts.each do |post| %>
    <tr>
      <td><%= post.title %></td>
      <td><%= post.text %></td>
    </tr>
  <% end %>
</table>
<h1>Hello,Rails</h1>
<%=link_to "My Blog",controller: "posts"%>

show.html.rb 文件是

show.html.rb file is

这是列出数据库信息的显示文件,直接从 url 访问时此文件出现问题 这是列出数据库中信息的显示文件,直接从 url 访问时出现问题 这是列出信息的显示文件从数据库,这个文件在直接从 url 访问时出现问题 这是从数据库中列出信息的显示文件,当从 url 直接访问时这个文件出现问题 这是从数据库中列出信息的显示文件,这个文件在访问时出现问题直接来自网址

this is show file for listing information from database,this file is giving problem when accessing from url directly this is show file for listing information from database,this file is giving problem when accessing from url directly this is show file for listing information from database,this file is giving problem when accessing from url directly this is show file for listing information from database,this file is giving problem when accessing from url directly this is show file for listing information from database,this file is giving problem when accessing from url directly

<p>
  <strong>Title:</strong>
  <%= @post.title  %>
</p>

<p>
  <strong>Text:</strong>
  <%= @post.text %>
</p>
<%= link_to 'Back', posts_path %>

这是列出数据库信息的显示文件,直接从 url 访问时此文件出现问题 这是列出数据库信息的显示文件,直接从 url 访问时此文件出现问题

this is show file for listing information from database,this file is giving problem when accessing from url directly this is show file for listing information from database,this file is giving problem when accessing from url directly

推荐答案

您需要提供一个 id.未找到记录,因为您提供了show"作为 ID.http://ruby.railstutorial.org/chapters/sign-up#code-user_show_action

You need to supply an id. A record isn't found because you are supplying "show" as the id. http://ruby.railstutorial.org/chapters/sign-up#code-user_show_action

示例:访问 posts/1 而不是 posts/show

example: visit posts/1 instead of posts/show

这篇关于ActiveRecord::RecordNotFound in PostsController#show的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆