SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Jul 2007
    Location
    Australia
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    RoR book - permalink field in table empty

    Hi All,
    I am going through Patrick Lenz's 'Build your own Ruby on Rails Web Apps' and i have an issue with adding new 'stories'.

    I appear to add the story fine, but when i look in the database, the permalink field for all the new stories has 'null' and randomly when i refresh my story page (which should show a random link to a story) sometimes it throws error:

    ActionController::RoutingError in Story#index
    story_url failed to generate from {:controller=>"story", :action=>"show", :permalink=>nil}, expected: {:controller=>"story", :action=>"show"}, diff: {:permalink=>nil}
    Extracted source (around line #2):
    1: link:
    2: <%= link_to @story.name, story_url(:permalink => @story.permalink) %>


    Which i am assuming is when it is trying to access one of the stories that doesn't have a permalink entry in the database.
    Any ideas as to why it isn't adding the permalink entry to the database?

    This is what i have done (copied straight from the book):
    • added the routing as stated, just after the first map.connect
      map.story 'story/show/:permalink', :controller => 'story', :action => 'show'
    • setup and run the migrate file
      class AddPermalinkToStories < ActiveRecord::Migration
      def self.up
      add_column :stories, :permalink, :string
      end
      def self.down
      remove_column :stories, :permalink
      end
      end
    • and added the show method to the story_controller
      def show
      @story = Story.find_by_permalink(params[:permalink])
      end

    Is there something more I need to do? Any help would be appreciated :)

    Thanks in advance,
    Katrina.

  2. #2
    SitePoint Member
    Join Date
    Feb 2007
    Location
    Wiesbaden, Germany
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Katrina,

    the automated generation of permalinks isn't implemented before Chapter 9 (page 300 of the printed book).

    When the permalink attribute is added in Chapter 7, the page following the migration has instructions on how to add them manually for the time being (page 198 of the printed book).

    Please let me know if this solves your issue.

    Best,
    Patrick

  3. #3
    SitePoint Member
    Join Date
    Jul 2007
    Location
    Australia
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Patrick,

    Thanks for the quick response. I'm up to the end of chapter 8, so I am pretty confident that this is why i was having problems. That will teach me for not looking ahead to the next chapter and seeing the nice big title 'Auto-generating Permalinks'. I just assumed when it said (p262 pdf version, don't have book on me right now) "3. Delete all data from database, and add your data from scratch via the application" that this meant that the application should be at the stage that Permalinks should have been put into the database. I'm glad it isn't but, as I was starting to think there was a lot more ruby magic happening that I couldn't see in the code.

    Thanks again for the well-written book, can't wait to get some more spare time so I can continue going through it

    Cheers,
    Katrina.

  4. #4
    SitePoint Member
    Join Date
    Jul 2009
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This also got me. My problem was I had created a lot of records in my DB by extracurricular noodling around. It seems that having only *one* record with a nil permalink is enough.

    So I did this from a prompt:

    s = Stories.findall)
    for x in s
    if x.permalink = nil
    Stories.delete(x)
    end
    end


    ... and that weeded out all those records. Now it works.

    SF


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
  •