SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Aug 2010
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Chapter 7 Undefined method 'name' error

    Hi guys, I am a newbie and have run into a wall in Chapter 7, Ajax and Web 2.0.
    I am trying to display the page based on ID number, for example by loading http://localhost:3000/stories/2
    However, I keep getting the error:

    NoMethodError in Stories#show
    undefined method `name' for nil:NilClass

    Extracted source (around line #1):

    1: <h2><%= @story.name %></h2>
    2: <p><%= link_to @story.link, @story.link %></p>
    Request

    Parameters:

    {"id"=>"1-sitepoint-forums"}


    I have been trying to find a solution on Google to no avail. I thought it might have to do with no values for 'name' being passed correctly or some problem with the routes. I tried playing around with both, but have not been able to find a solution.

    Any help is greatly appreciated.

  2. #2
    SitePoint Zealot
    Join Date
    Feb 2003
    Location
    Akron, OH, USA
    Posts
    106
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Post your stories controller and we can take a look at that and maybe figure out what is wrong!

  3. #3
    SitePoint Member
    Join Date
    Aug 2010
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Scannon, first of all thanks very much for responding! I am really going nuts..
    I have pasted the code to my story controller below of what I have done so far



    class StoriesController < ApplicationController
    before_filter :login_required, nly => [ :new, :create ]
    def index
    @story = Story.findfirst, rder => 'RANDOM()')
    end
    def new
    @story = Story.new
    end
    def create
    @story = @current_user.stories.build params[:story]
    @story = Story.new(params[:story])
    if @story.save
    flash[:notice] = 'Story submission succeeded'
    redirect_to stories_path
    else
    render :action => 'new'
    end
    def show
    @story = Story.find(params[:id])
    end

    end
    end


    Thanks!

  4. #4
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have you got RESTFUL routing enabled for this controller. Can you post your config/routes.rb file?

  5. #5
    SitePoint Member
    Join Date
    Aug 2010
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi ReggieB, I have pasted below the entire code of the config/routes.rb file..Thanks!!!


    ActionController::Routing::Routes.draw do |map|
    # The priority is based upon order of creation: first created -> highest priority.

    # Sample of regular route:
    # map.connect 'products/:id', :controller => 'catalog', :action => 'view'
    # Keep in mind you can assign values other than :controller and :action

    # Sample of named route:
    # map.purchase 'products/:id/purchase', :controller => 'catalog', :action => 'purchase'
    # This route can be invoked with purchase_urlid => product.id)

    # Sample resource route (maps HTTP verbs to controller actions automatically):
    # map.resources roducts

    # Sample resource route with options:
    # map.resources roducts, :member => { :short => :get, :toggle => ost }, :collection => { :sold => :get }

    # Sample resource route with sub-resources:
    # map.resources roducts, :has_many => [ :comments, :sales ], :has_one => :seller

    # Sample resource route with more complex sub-resources
    # map.resources roducts do |products|
    # products.resources :comments
    # products.resources :sales, :collection => { :recent => :get }
    # end

    # Sample resource route within a namespace:
    # map.namespace :admin do |admin|
    # # Directs /admin/products/* to Admin::ProductsController (app/controllers/admin/products_controller.rb)
    # admin.resources roducts
    # end

    # You can have the root of your site routed with map.root -- just remember to delete public/index.html.
    map.root :controller => "stories"

    # See how all your routes lay out with "rake routes"

    # Install the default routes as the lowest priority.
    # Note: These default routes make all actions in every controller accessible via GET requests. You should
    # consider removing or commenting them out if you're using named routes and resources.
    map.resources :stories, :has_many => :votes
    map.connect ':controller/:action/:id'
    map.connect ':controller/:action/:id.:format'
    map.resource :session
    end

  6. #6
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That looks fine.
    Code:
    map.resources :stories, :has_many => :votes
    That line is enabling the restful charlies.

    However, I think I've spotted the problem. It is this:
    Code:
    Parameters:
    
    {"id"=>"1-sitepoint-forums"}
    That should be
    Code:
    Parameters:
    
    {"id"=>"1"}
    I can't see how you can get that unless you the url you send via GET is:

    http://localhost:3000/stories/1-sitepoint-forums

    Which doesn't make sense.

    What do you get if you enter the url:

    http://localhost:3000/stories/show/1


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •