SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Aug 2008
    Location
    Georgia, USA
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy uninitialized constant StoriesController

    Hello Guys:

    I am really new in RoR, two weeks old. I am studying on my own using the Simply Rails 2 book by Patrick. I am in Chapter 6, and following the instructions, this is my StoriesController file:

    class StoriesController < ApplicationController
    def index
    @story = Story.find( :first, : order => 'RANDOM()')
    #Comment: I left a space between ( and : and : and order since otherwise a figuring would show.
    end

    #Connecting the new.html.erb file:
    def new
    @story = Story.new
    end

    def create
    @story = Story.new(params[:story])
    @story.save
    #this is a url helper
    redirect_to stories_path
    end
    end
    This is the error that I am getiing:

    NameError in StoriesController#new

    uninitialized constant StoriesController

    RAILS_ROOT: C:/InstantRails-2.0-win/rails_apps/shovell
    Now, if I use the StoriesController generated when I ran
    rails shovell
    everything works well. However, Patrick in his book in chapter 6, apparently starts building this file a method at a time. This is the way I want to do it also because I think I would learn by doing it this way. Nonetheless, I am stuck.

    Please help me anyone.
    Thanks.
    --Willie

  2. #2
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not entirely sure what the problem is here. Where are you getting the error? What do you do to generate the error?

    Rails files work as a unit. You need to create the web application first (e.g. via rails application_name) and then use individual files within the structure created. So controller files need to live in the app/controllers folder. If you take one of those files and move it to another location and try to run it, it will most likely fail.

    However, there is an error in you code. There is no problem putting a space between the opening parenthesis and the first colon, but there is a problem putting a space between the colon and the word 'order'.

    A colon at the start of a lower case word tells the system that this is a 'symbol'. So
    Code:
    :order
    is a symbol, where as
    Code:
    order
    is a variable name. Lone Colons have their own meaning (e.g. within ternary operators).

    Therefore, using ": order" instead of
    Code:
    :order
    will cause an error.

  3. #3
    SitePoint Member
    Join Date
    Aug 2008
    Location
    Georgia, USA
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello ReggieB:

    Thanks for your response. I have solved that problem already. The way I did it was not that good, but I was desperate, I created the application again.

    Now I am facing this new problem:

    NoMethodError in Stories#index

    Showing stories/index.html.erb where line #9 raised:

    You have a nil object when you didn't expect it!
    You might have expected an instance of Array.
    The error occurred while evaluating nil.each

    Extracted source (around line #9):

    6: <th>Link</th>
    7: </tr>
    8:
    9: <&#37; for story in @stories %>
    10: <tr>
    11: <td><%=h story.name %></td>
    12: <td><%=h story.link %></td>

    RAILS_ROOT: C:/InstantRails-2.0-win/rails_apps/shovell
    Application Trace | Framework Trace | Full Trace

    app/views/stories/index.html.erb:9:in `_run_erb_47app47views47stories47index46html46erb'

    If you or someone else can help me figure this out, it would be greatly appreciated.

    Thanks.
    --Willie

  4. #4
    SitePoint Member
    Join Date
    Aug 2008
    Location
    Georgia, USA
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello forums:

    I solved the above error.

    In my stories_controller.rb I had this line:

    def index
    @story = Story.findall, rder => 'RANDOM()')
    end
    I had to change @story to @stories:

    def index
    @stories = Story.findall, rder => 'RANDOM()')
    end
    This is because in my index.html.rb file I have this line:

    <% for story in @stories %>
    They have to match in both files.

    I hope this helps someone in the future.
    I will come back with more errors.

    Thanks.
    --Willie


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
  •