I have been doing some hacking on Type Aloud and I was looking for an answer to a question that was bugging me: what is the best way to use link_to with a vanity URL setup? I asked my question over at Stack Overflow and I figured I would post the answer to the question here in case anyone was looking as I was.
Inside of my application the route setup is quite simple.
match '/:id', :to => "users#show" match '/:user_id/:id', :to => "stories#display"
Now I want to be able to make a very simple call inside of my view logic which will print out a pretty URL. Since I am using resource definitions for several controllers I cannot use the normal Rails URL helpers. The answer to my question is actually quite simple, and I already have used it before, but what I wasn’t doing was correctly passing through the objects into the helper. Here are the changes that you need to make to those routes above.
match '/:id', :to => "users#show", :as => "vanity_show_user" match '/:user_id/:id', :to => "stories#display", :as => "vanity_show_users_story"
And finally inside of your view you would build both links like the following.
<%= link_to @story.name, vanity_show_users_story_path(@user, @story) %> <%= link_to @user.name, vanity_show_user_path(@user) %>
I hope that this helped you out as much as it helped me! Kudos to the amazing Ryan Bigg for the original answer.
The past couple of weeks I have been doing some research because I am planning on working on a FastCGI Model-View-Controller framework for developing web application services in C++. I am a C++ engineer by day and have been working with the language for nigh on a decade. The brief research I was able to do has shown me that there are no DataMapper or Active Record open source libraries available.
What Exactly Is The N+1 Query Problem?
When using an abstract framework to build a data model for use with an SQL-esque language you generally need to build a set of classes to model the schema in said language. For Type Aloud, lets say that I want to display all of the stories in a set of categories after you get them back based upon two slugs taken from the site’s URL.
SELECT id FROM categories WHERE slug='sci-fi' OR slug='fantasy' SELECT id, name FROM stories WHERE category_id = 0 OR category_id = 1
How many queries will be run? At most there will be two queries, but what about if none of the categories match the two slugs provided? That first dangling select query will be executed each and every time even if there are no matches to search against. How can we fix this?
SELECT * FROM stories INNER JOIN categories ON stories.category_id = categories.id AND (categories.slug = 'sci-fi' OR categories.slug = 'fantasy')
This type of explicit join will allow you to execute a single SQL query and, in the long run, will save you thousands of CPU cycles with your web applications. This type of data mapper already exists in Ruby on Rails using the Data Mapper gem which is freely available. But as of right now there is no such framework available (unless I am mistaken) in C++ which gives you the power and flexibility to build your data models and eat your cake too. My first plan in developing an MVC framework is to work on architecting a proper data mapper library for C++.
Anyone else game?
I decided to take some long needed rest away from the computer and enjoy my Thanksgiving holiday with friends and family. This has put some of my development initiatives on the back burner. I am aiming to gear up this weekend and hopefully be able to release a little tidbit at the end of this weekend just for the special someones who sign up for the service early. The current goal is to have story and chapter submission, viewing and commenting up by the end of this weekend.
I will continue the remaining work and slowly release the bits out to my testers. I expect (and hope) those of you that have signed up are ready and willing to start writing some short stories, and eventually, poems!
I have arrived at one of the bullet points for Type Aloud where I must implement the ability for one user to follow another user, similar to being a friend on Facebook or exactly the same as becoming a follower on Twitter. But I came to a dilemma that needed to be solved: What if someone only wanted to follow one particular story from an author?
The first thing that came to mind (and ultimately stuck) was to have two different type of subscription models – the first would allow you to physically subscribe to a story and be notified when a new chapter was posted by said author. This subscription would keep track of reading point on a particular chapter as well as what chapter was the last one that you read. This may seem very trivial for web browser users but when you plan on handling Amazon Kindle traffic this becomes very important. The second subscription model would be a “Fan” and this is where you want to hear absolutely everything from the author – if they decide to post a new story, you hear about it, or if they decide to write a poem, the same deal.
When thinking through this I was trying to take into consideration user interaction. How would the button placement work for these two particular types of subscriptions? If someone wanted to cease being notified from said author, how exactly would that work? And finally, if someone subscribes to another author’s story and then becomes a fan what happens if they eventually cease the fanship – do we maintain the original state of subscription?
I would be interested what you all have to think.