The Battle Between the Right Way and The Working Way
I’ve this got awesome full stack framework at my disposal, various books, videos, articles and open source examples, yet rolling my own commenting system was a challenge in and of itself (otsn: is this the same as ‘unto itself’). I thought I had things working for me with my nested routes and simplistic system, however I guess it goes to show you that even the simplest systems can be massively complicated and confusing in the right hands.
Posting to Nested Routes and Displaying Errors
I’ve got some nested routes:
map.resources :posts, :collection => {:feed => :get}, :has_many => :comments
Posts have many comments as you’d expect. So this gives me nice URLs like /posts/5/comments/new and named routes like new_post_comment_path. Easy enough. The problem I ran into was following this common rails convention:
- Form is displayed when GETting a Controller’s new action
- User enters information in form
- Validation fails
- “new” action is re-rendered from the Controller’s create action
This is simple and not even worth mentioning for most scenarios, however I got myself in a bind when I tried to apply it to my posts/comments scenario.
- Post’s “show” action is rendered (PostsController)
- Comment’s form is displayed (PostsController)
- User enters information into Comment’s form and it’s posted to /posts/:post_id/comments (CommentsController)
- Validation fails and “form” has to be re-rendered (CommentsController)
- WTF now?
I can’t just re-render the PostsController’s show action (I messed around with rendering the template with render :template but that didn’t really work). I can’t render the PostsController’s show action again and hop down to the Comment’s form (I tried using :anchor and all sorts of shit, no luck). At this point I was really stuck, couldn’t really figure out what to do next, rails confidence shattered, ego bruised, internet more browsed… eventually I gave up for a few days.
The Right Way and the Working Way
There’s something about Rails that makes me want to do things the “right” way. I don’t even know what that means half the time, but something about the community, the examples, the general vibe of everything suggests that there is usually a “right” way. Sometimes I think I get a little too caught up in this. Coming from PHP where there isn’t really a “right” way, I think part of me is searching for some structure and validation where I can have some confirmation that not just BSing my way through a problem is a good solution.
I got to a point with this problem where I basically gave up. I shelved the problem for about three days, just kinda ignored the whole thing. When I finally came back to it, I had this genius idea to not be fancy and just display the Comment’s form on it’s own if validation fails. This works in that it solves my problem, it’s not totally out of this world usability wise and it has allowed me to move on to solving other problems and making new improvements.
When you’re at a crossroads of doing something the “right way” and the “working way” you need to ask yourself if it’s worth forgoing the “right way” in favor of the “working way”, often times you’ll find that the “working way” is “right” enough and you’ll save yourself a lot of time and frustration, saving your energy to tackle more important problems.
otsn: off topic side note, LDO












