user3301669
user3301669

Reputation:

ActiveRecord::RecordNotFound in PostsController#show

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!'

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

my controller file is

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 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 file is

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 %>

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

Upvotes: 0

Views: 3076

Answers (1)

Jaron Gao
Jaron Gao

Reputation: 466

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

example: visit posts/1 instead of posts/show

Upvotes: 1

Related Questions