4

i have been learning rails through

http://guides.rubyonrails.org/getting_started.html.

I came across a error while performing save data in controller. The error that comes up when running the blog is :"The action 'show' could not be found for PostsController"

**

My code for posts_controller.rb is

**

class PostsController < ApplicationController
def new
end
def create
@post=Post.new(params[:post].permit(:title,:text))
@post.save
redirect_to @post
end

private
def post_params
params.require(:post).permit(:title,:text)
end

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

**

My code for show.html.rb is

**

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

**

The code for create_posts.rb

**

class CreatePosts < ActiveRecord::Migration
  def change
    create_table :posts do |t|
      t.string :title
      t.text :text

      t.timestamps
    end
end

Please help me out why this error is coming up

neha sharma
  • 117
  • 1
  • 3
  • 11
  • http://stackoverflow.com/questions/17965341/nomethoderror-in-postsshow delete the duplicate of a question :) – AKovtunov Aug 01 '13 at 11:26

2 Answers2

7

PostsController#show method should be public.

class PostsController < ApplicationController
  def new
  end

  def create
    @post=Post.new(params[:post].permit(:title,:text))
    @post.save
    redirect_to @post
  end

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

private

  def post_params
    params.require(:post).permit(:title,:text)
  end

end
Marek Lipka
  • 50,622
  • 7
  • 87
  • 91
  • 1
    Any method below the private declaration is considered private, if you want a method to be public you need to declare it above this or write the keyword public before your method. – yarakyo Aug 01 '13 at 11:18
  • @nehasharma and your backtrace? – Marek Lipka Aug 01 '13 at 11:21
1

Why did you put your show action in the Private? Just put it out of the private.

def new
  @post = Post.new
end
def create
 @post=Post.new(params[:post].permit(:title,:text))
 if @post.save
   redirect_to @post
 else
   render 'new'
 end
end

def show
  @post=Post.find_by_id(params[:id])
  if @post.blank?
    flash[:error] = 'The Post not found in the database'
    redirect_to root_path 
  end
end

private

def your_private_functions

end
Bachan Smruty
  • 5,686
  • 1
  • 18
  • 23
  • redirect_to posts_path if @post.blank? here posts_path is an example('posts#index'), you can redirect to some where else as well... like 'root_path'. BTW, the post you are looking for does not exist in your database. – Bachan Smruty Aug 01 '13 at 11:29