SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Oct 2005
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Chapter 6: Story Controller Won't Display

    I'm having trouble getting the Story Controller to display correctly; here is the error page I get when I try to get it to run locally:



    Here is the code for story_controller.rb:
    Code:
    class StoryController < ApplicationController
      def index
        @story = Story.find(:first, :order=>'RAND()')
        def new
          @story = Story.new(params[:story])
          if request.post?
            @story.save
            redirect_to :action => 'index'
      end
    end
    And here is the code for new.rhtml

    Code:
    <% form_for :story do |f| %>
    <p>
      name:<br />
      <%= f.text_field :name %>
    </p>
    <p>
      link:<br />
      <%=f.text_field :link %>
    </p>
    <p>
      <%= submit_tag %>
    </p>
    <% end %>
    Thanks for your assistance.

  2. #2
    SitePoint Member skrolikowski's Avatar
    Join Date
    Nov 2006
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're forgetting to "end" something somewhere.

  3. #3
    Awesome Addict
    Join Date
    Mar 2004
    Location
    Toronto, Canada
    Posts
    326
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    [QUOTE=pdenlinger;3314169]
    Code:
    class StoryController < ApplicationController
      def index
        @story = Story.find(:first, :order=>'RAND()')
        def new
          @story = Story.new(params[:story])
          if request.post?
            @story.save
            redirect_to :action => 'index'
      end
    end
    You've got a method nested within a method. "index" and "new" should be individual methods. Here's the fix:

    Code:
    class StoryController < ApplicationController
      def index
        @story = Story.find(:first, :order=>'RAND()')
      end
      
      def new
        @story = Story.new(params[:story])
        if request.post?
          @story.save
           redirect_to :action => 'index'
        end
      end

  4. #4
    Awesome Addict
    Join Date
    Mar 2004
    Location
    Toronto, Canada
    Posts
    326
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pdenlinger View Post
    Code:
    class StoryController < ApplicationController
      def index
        @story = Story.find(:first, :order=>'RAND()')
        def new
          @story = Story.new(params[:story])
          if request.post?
            @story.save
            redirect_to :action => 'index'
      end
    end
    You've got a method nested within a method. "index" and "new" should be individual methods. Here's the fix:

    Code:
    class StoryController < ApplicationController
      
      def index
        @story = Story.find(:first, :order=>'RAND()')
      end
      
      def new
        @story = Story.new(params[:story])
        if request.post?
          @story.save
           redirect_to :action => 'index'
        end
      end
    
    end

  5. #5
    SitePoint Member
    Join Date
    Oct 2005
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK, thank you.

    I'm working my way through the Pickaxe book to get a better handle on Ruby.


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
  •