Beginer Issue: NoMethodError in Posts#show

378 vues
Accéder directement au premier message non lu

Jeremy Guilbert

non lue,
8 juil. 2014, 23:36:1208/07/2014
à rubyonra...@googlegroups.com
Hello,

I'm new at Ruby on Rails, I'm using :
-Ubuntu 14.04 LTS
-Rails 4.1.2
-Ruby 2.1.2p95
-rvm 1.25.27

I'm actually following the rails guides : http://guides.rubyonrails.org/v4.0.6/getting_started.html#getting-up-and-running
Everything was going well until the point : 5.7 Showing points.
I have checked my files several times and I can't figure out where is my mistake, I can submit a post and then there is no display I get the error :

NoMethodError in Posts#show

Showing /home/jeremy/projet/blog/app/views/posts/show.html.erb where line #3 raised:

undefined method `title' for nil:NilClass

Extracted source (around line #3):

1
2
3
4
5
6
          
<p>
<strong>Title:</strong>
<%= @post.title %>
</p>
<p>
Following you'll find my files :
-config/routes.rb
Rails.application.routes.draw do
 
get 'welcome/index'

 
# The priority is based upon order of creation: first created -> highest priority.
 
# See how all your routes lay out with "rake routes".
  resources
:posts
 
# You can have the root of your site routed with "root"
  root
'welcome#index'

 
# Example of regular route:
 
#   get 'products/:id' => 'catalog#view'

 
# Example of named route that can be invoked with purchase_url(id: product.id)
 
#   get 'products/:id/purchase' => 'catalog#purchase', as: :purchase

 
# Example resource route (maps HTTP verbs to controller actions automatically):
 
#   resources :products

 
# Example resource route with options:
 
#   resources :products do
 
#     member do
 
#       get 'short'
 
#       post 'toggle'
 
#     end
 
#
 
#     collection do
 
#       get 'sold'
 
#     end
 
#   end

 
# Example resource route with sub-resources:
 
#   resources :products do
 
#     resources :comments, :sales
 
#     resource :seller
 
#   end

 
# Example resource route with more complex sub-resources:
 
#   resources :products do
 
#     resources :comments
 
#     resources :sales do
 
#       get 'recent', on: :collection
 
#     end
 
#   end

 
# Example resource route with concerns:
 
#   concern :toggleable do
 
#     post 'toggle'
 
#   end
 
#   resources :posts, concerns: :toggleable
 
#   resources :photos, concerns: :toggleable

 
# Example resource route within a namespace:
 
#   namespace :admin do
 
#     # Directs /admin/products/* to Admin::ProductsController
 
#     # (app/controllers/admin/products_controller.rb)
 
#     resources :products
 
#   end
end
-app/controllers/posts_controller.rb
class PostsController < ApplicationController
   
def new
   
end

   
def create
     
@post = Post.new(post_params)

       
@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
-
app/views/posts/new.html.erb
<h1>New Post</h1>
<%= form_for :post, url: posts_path do |f| %>
 
<p>
   
<%= f.label :title %><br>
   
<%= f.text_field :title %>
 
</p>
 
 
<p>
   
<%= f.label :text %><br>
   
<%= f.text_area :text %>
 
</p>
 
 
<p>
   
<%= f.submit %>
 
</p>
<% end %>
-db/migrate/20140630133544_create_posts.rb
class CreatePosts < ActiveRecord::Migration
 
def change
    create_table
:posts do |t|
      t
.string :title
      t
.text :text

      t
.timestamps
   
end
 
end
end
-
app/views/posts/show.html.erb
<p>
 
<strong>Title:</strong>
 
<%= @post.title %>
</p>
 
<p>
 
<strong>Text:</strong>
 
<%= @post.text %>
</p>

So if you can tell me what's wrong I'll be very happy, because I really don't get it.
Thanks.




BalaRaju Vankala

non lue,
9 juil. 2014, 02:49:2709/07/2014
à rubyonra...@googlegroups.com
Hey Jeremy, 

Your Code is Good. Please add below code in to your show.html.erb

<% if @post.present? %>
   <p>
  <strong>Title:</strong>
  
<%= @post.title %>
</p>
 
<p>
  
<strong>Text:</strong>
  
<%= @post.text %>
</p>
<% else %>
  <p>There is no posts to show</p>
<% end %>

 SHOW page needs POST ID. You entered wrong ID, I  think with that ID there is no post exist in the database. 

Please CHECK in the rails console also 

Post.find(<YOUR ID>)

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/4ca41298-1a4f-4730-8cc1-bf928a8c28b9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
----------------------------------------------------------------------------------------------------
Thank You.

Best Wishes,
 
BalaRaju Vankala,
8886565300.


Yong Gu

non lue,
9 juil. 2014, 03:13:4909/07/2014
à rubyonra...@googlegroups.com
Hi,

Move you show method above the private, just like your new and create method.

Jeremy Guilbert

non lue,
9 juil. 2014, 04:37:4609/07/2014
à rubyonra...@googlegroups.com
Ok thank you for your answers.
Répondre à tous
Répondre à l'auteur
Transférer
0 nouveau message