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*):
123456
<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.
--
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-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
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.