Therafer
Therafer

Reputation: 89

Couldn't find Post with id=show?

I have been working on the Ruby on Rails v.4.0.0 guide and just went through the last of the code. But I have 3 problems that i think might come from 1 source. My SHOW method doesn't seem to work for whatever reason. I have a "show" view but it gives me the error "Couldn't find Post with id=show". and tells me that line 35 of my PostsController is wrong. I have looked for a while and can't seem to find anyone with a similar enough propblem. So here is the Controller and view.

Controller:

    1 class PostsController < ApplicationController
    2
    3   http_basic_authenticate_with name: "dhh", password: "secret",
    4 except: [:index, :show]
    5 
    6   def new
    7     @post = Post.new
    8   end
    9 
    10   def create
    11     @post = Post.new(params[:post])
    12 
    13     if @post.save
    14       redirect_to @post
    15     else
    16       render 'new'
    17     end
    18   end
    19                                                                    
    20   def edit
    21     @post = Post.find(params[:id])
    22   end
    23 
    24   def update
    25     @post = Post.find(params[:id])
    26 
    27     if @post.update(params[:post].permit(:title, :text))
    28       redirect_to @post
    29     else
    30       render 'edit'
    31     end
    32   end
    33 
    34   def show
    35     @post = Post.find(params[:id])
    36   end
    37 
    38   def index
    39     @post = Post.all
    40   end
    41 
    42   def destroy
    43     @post = Post.find(params[:id])
    44     @post.destroy
    45 
    46     redirect_to posts_path
    47   end
    48 
    49   private
    50     def post_params
    51       params.require(:post).permit(:title, :text)
    52     end
    53 end

View:

    1 <%= @post.each do |post| %>
    2 <p>
    3   <strong> Title: </strong> 
    4   <%= @post.title %>
    5 </p>
    6 
    7 <p> 
    8   <strong> Text: </strong>
    9   <%= @post.text %>
    10 </p>
    11 
    12 <%end%>
    13 
    14 <h2> Comments </h2>
    15 <%= render @post.comments %>
    16 
    17 <h2>Add a comment:</h2>
    18 <%= render "comments/form" %>
    19 
    20 <%= link_to 'Back to Posts', posts_path %>
    21 | <%= link_to 'Edit Post', edit_post_path(@post) %>

The full error is :

ActiveRecord::RecordNotFound in PostsController#show Couldn't find Post with id=show

Note: I am dyslexic so its possibly just a spelling error...

Upvotes: 1

Views: 5336

Answers (3)

Rachid Calazans
Rachid Calazans

Reputation: 9

Do You placed the PostController in routes.rb?

resources :post

Upvotes: 0

makewavesnotwar
makewavesnotwar

Reputation: 1005

I can't comment so this is an answer for now but can you confirm that the URL you are going to contains the id of the post you're looking for? If it's looking for a post with ID of 'show' it sounds like you might be going to yourUrl/posts/show instead of yourUrl/posts/1

Upvotes: 2

ventsislaf
ventsislaf

Reputation: 1671

params[:id] is equal to "show" instead of some real ID. Probably you are trying to access wrong url like /posts/show instead of /posts/1 or whatever the ID is.

Upvotes: 5

Related Questions