enough theory, let's fix some bugs!

9 views
Skip to first unread message

Denis Haskin

unread,
Jun 27, 2012, 7:41:04 AM6/27/12
to boston-rubygroup
Actually, I'm really enjoying the 'modularity' discussion... but I'd like to get out of this trench I'm in, so:

I'm migrating a project from one codebase to another to clean up a lot of accumulated cruft, shift to a new presentation layer, etc.

One of the relationships is the canonical User has_many Order, which I have expressed as nested resources in routes.rb:

    resources :users do
      resources :orders
    end

and so I have the expected named route user_orders_path(@user).  It works just fine if the user has orders, but if the user has no orders, trying to render the route gives the following error:

    ActionController::RoutingError (No route matches {:controller=>"orders", 
    :user_id=>#<User id: 472, account_id: nil, first_name: "Denis", last_name: 
    "Haskin", ...etc... , last_sign_in_ip: "127.0.0.1">})

I'm sore of at a loss to explain (and fix) this, and I was hoping this might ring a bell with someone.  I tested this in a new empty project and of course it works fine.  I guess the next thing I'll try is to strip down as much as I can about these models and the controller, and start adding pieces back until the error recurs.  Because I'm migrating over existing code, it's very difficult to isolate this to a small piece of example code.

(At the risk of possibly confusing the issue, I have started using Ryan Bates' cancan[1] in this project and I'm a little suspicious that it might be playing a role here, but I did strip it out and test without it, with no change in this incorrect behavior.)

Thanks for any help, suggestions on how to further investigate, etc (yes, I've also asked on StackOverflow)...


--
Denis Haskin

Maurício Linhares

unread,
Jun 27, 2012, 8:12:33 AM6/27/12
to boston-r...@googlegroups.com
What happens when you use @user.id in there?


-
Maurício Linhares
http://techbot.me/ - http://twitter.com/#!/mauriciojr
> --
> You received this message because you are subscribed to the Boston Ruby Group mailing list
> To post to this group, send email to boston-r...@googlegroups.com (mailto:boston-r...@googlegroups.com)
> To unsubscribe from this group, send email to boston-rubygro...@googlegroups.com (mailto:boston-rubygro...@googlegroups.com)
> For more options, visit this group at http://groups.google.com/group/boston-rubygroup



Daniel Choi

unread,
Jun 27, 2012, 8:25:10 AM6/27/12
to boston-r...@googlegroups.com, dhc...@gmail.com
On Wed, Jun 27, 2012 at 07:41 AM, Denis Haskin <de...@constantorbit.com> wrote:


> and so I have the expected named route user_orders_path(@user). It works
> just fine if the user has orders, but if the user has no orders, trying to
> render the route gives the following error:
>
> ActionController::RoutingError (No route matches
> {:controller=>"orders",
> :user_id=>#<User id: 472, account_id: nil, first_name: "Denis",
> last_name:
> "Haskin", ...etc... , last_sign_in_ip: "127.0.0.1">})


In rails c, what does `User.find(472).to_param` return?

You can also test if the routes are being drawn correctly in `rails c`

In rails c:

u = User.find(472) # <= user with no orders
app.user_orders_path(u)
# see what you get
u.to_param
# see what you get

# repeat above with a user that doesn't trigger this bug




Denis Haskin

unread,
Jun 27, 2012, 9:13:39 AM6/27/12
to boston-r...@googlegroups.com
Bingo!  user.rb has

  def to_param
    login
  end

removing that makes it work.  I had started to suspect that, as I was just starting to work around by hard-coding "/users/#{current_user.to_param}/orders" (oh the horror, but I didn't want to stay stuck on this forever).

I'm not sure I understand why that broke things.  Can someone explain? (I know, I should work it out myself but I can't get to that until this evening and I'm impatient ;-) ).

Also, what is the current school of thought?  People used to want to use to_param to make "SEO-friendly" URLs; I remember it causing me a lot of grief several years ago.

Thanks!

--
Denis Haskin




--
You received this message because you are subscribed to the Boston Ruby Group mailing list
To post to this group, send email to boston-r...@googlegroups.com
To unsubscribe from this group, send email to boston-rubygro...@googlegroups.com

Wyatt Greene

unread,
Jun 27, 2012, 9:41:56 AM6/27/12
to boston-r...@googlegroups.com
If I want SEO-friendly URLs, I use https://github.com/norman/friendly_id

Braulio Carreno

unread,
Jun 27, 2012, 9:54:41 AM6/27/12
to Boston Ruby Group
That grief made me switch to the simpler https://github.com/Sutto/slugged

Brendan Kemp

unread,
Jun 27, 2012, 10:33:29 AM6/27/12
to Boston Ruby Group
> That grief made me switch…

Grief? I like the look of slugged, I'm just curious what grief you had
with friendly_id.

Braulio Carreno

unread,
Jun 27, 2012, 10:44:32 AM6/27/12
to Boston Ruby Group
Errors when trying to make it work with Rails 3

Daniel Choi

unread,
Jun 27, 2012, 11:24:41 AM6/27/12
to boston-r...@googlegroups.com, dhc...@gmail.com
On Wed, Jun 27, 2012 at 09:13 AM, Denis Haskin <de...@constantorbit.com> wrote:

> Bingo! user.rb has
>
> def to_param
> login
> end
>
> removing that makes it work. I had started to suspect that, as I was just
> starting to work around by hard-coding
> "/users/#{current_user.to_param}/orders" (oh the horror, but I didn't want
> to stay stuck on this forever).
>
> I'm not sure I understand why that broke things. Can someone explain? (I
> know, I should work it out myself but I can't get to that until this
> evening and I'm impatient ;-) ).


Was the original User#to_param (calling user#login) returning nil for
User 472? If #to_param returns nil, you're going to see the
ActionController::RoutingError.





------------------------------------------------------------------------
----
Sent from vmail
https://github.com/danchoi/vmail

Denis Haskin

unread,
Jun 27, 2012, 11:31:12 AM6/27/12
to boston-r...@googlegroups.com
That's it.  The whole no-orders issue was a complete red herring.  It wasn't whether or not a user had orders, it was whether it had the necessary attributes for #to_param to succeed.  This just happened to have been the first time I had tried to use routes that depended on #to_param.

Thanks, I can sleep tonight.


--
Denis Haskin




Daniel Choi

unread,
Jun 27, 2012, 11:35:28 AM6/27/12
to boston-r...@googlegroups.com, dhc...@gmail.com
Join BostonRB: so you can sleep more


On Wed, Jun 27, 2012 at 11:31 AM, Denis Haskin <de...@constantorbit.com> wrote:

> from: Denis Haskin <de...@constantorbit.com>
> date: Wed, Jun 27 11:31 AM -04:00 2012
> to: boston-r...@googlegroups.com
> reply-to: boston-r...@googlegroups.com
> subject: Re: [boston.rb] enough theory, let's fix some bugs!

Denis Haskin

unread,
Jun 27, 2012, 11:39:56 AM6/27/12
to boston-r...@googlegroups.com, dhc...@gmail.com
I try to come when I can!  Unfortunately, I almost always have a conflict on Tuesdays...  I'm hoping that in the fall my conflicting commitment will move and my Tuesdays will become more consistently free.


--
Denis Haskin


Wyatt Greene

unread,
Jun 27, 2012, 12:04:14 PM6/27/12
to boston-r...@googlegroups.com, dhc...@gmail.com
It's true. You can sleep in Ruby but not JavaScript.
Reply all
Reply to author
Forward
0 new messages