3

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 :-undefined method `title' for nil:NilClass

**

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 when I have defined title in create_posts.

neha sharma
  • 109
  • 1
  • 2
  • 10
  • 1
    Please add output to 'rake routes' command – user2503775 Jul 31 '13 at 08:39
  • can u elaborate your statement..where to add "rake routes" command – neha sharma Jul 31 '13 at 08:59
  • Type 'rake routes' at your terminal - in your project's folder, and add the output to your question.Then it will be easier to answer your question. – user2503775 Jul 31 '13 at 09:38
  • C:\Sites\blog>rake routes Prefix Verb URI Pattern Controller#Action welcome_index GET /welcome/index(.:format) welcome#index posts GET /posts(.:format) posts#index POST /posts(.:format) posts#create new_post GET /posts/new(.:format) posts#new edit_post GET /posts/:id/edit(.:format) posts#edit post GET /posts/:id(.:format) posts#show PATCH /posts/:id(.:format) posts#update PUT /posts/:id(.:format) posts#update DELETE /posts/:id(.:format) posts#destroy root GET / welcome#index – neha sharma Jul 31 '13 at 10:10
  • http://i.stack.imgur.com/mZrqI.png – neha sharma Jul 31 '13 at 10:12
  • I have added image and description of rake routes command ..now can you help me to debug the error – neha sharma Jul 31 '13 at 10:20
  • Your routes seems to be OK. Did you get the error when trying to create an object through the GUI? – user2503775 Jul 31 '13 at 10:26
  • If so, what your 'new' function is empty? – user2503775 Jul 31 '13 at 10:33
  • what should be there in "new" function – neha sharma Aug 01 '13 at 10:08
  • now some other error is coming up ...the error is "Unknown action The action 'show' could not be found for PostsController" – neha sharma Aug 01 '13 at 10:26

2 Answers2

15

All methods defined after private are accessible only internally. Move the show method above private. And make sure you have a file called app/views/posts/show.html.erb and not .rb

Good luck!

Anil
  • 3,829
  • 1
  • 17
  • 28
  • Now you are into views. Make sure you have a file called `app/views/posts/show.html.erb` and not `.rb`. – Anil Aug 01 '13 at 11:30
  • I edited the answer to reflect the view gotcha also. You are welcome. – Anil Aug 01 '13 at 11:33
  • you said to save the file with extension .erb not .rb. what is the difference between the two – neha sharma Aug 01 '13 at 11:45
  • file with extension erb(embedded ruby) allows you put ruby code in html file whereas file with extension rb does not. – vikram Aug 01 '13 at 11:50
  • Yes, @vikram is correct. As the missing template warning lists `:handlers=>[:rjs, :rhtml, :builder, :rxml, :erb]`, rails is looking for one of those extensions with the expected name in the views directory. It will also process a plain old .html file. A .erb file is first processed by ruby, and then rendered as an html. A .rb file is pure ruby. Look at controllers, models, and helpers. They are all .rb . I hope this helps. – Anil Aug 01 '13 at 12:33
0
# Make sure that you trying to access show method before the declaration of private as we can't access private methods outside of the class.

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


def index
  @posts = Post.all
end

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

  if @post.update(params[:post].permit(:title, :text))
    redirect_to @post
  else
    render 'edit'
  end
end

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

//vKj

Vinod Joshi
  • 7,094
  • 46
  • 49