开发者

Why is this route in Rails 3 with a custom action not working? (better way?)

I have the following code in my routes.rb:

match 'users/linkedin' => "users#linkedin", :as => :register_linkedin

My expectation is that when I have a redirect_to register_linkedin_url, I will be redirected to domain.com/users/linkedin.

That should then result in Controller Users with action linkedin being executed.

This is what I get in the logs:

Redirected to http://localhost:3000/users/linkedin Completed 302 Found in 28333ms

Started GET "/users/linkedin" for 127.0.0.1 at Thu Apr 14 01:12:01 -0700 2011 Processing by UsersController#show as HTML

Parameters: {"id"=>"linkedin"} Completed in 94ms

This is what I get in 'rake routes':

register_linkedin

/u开发者_Go百科sers/linkedin(.:format)

{:action=>"linkedin", :controller=>"users"}

So the routes aren't working properly. How do I address this?


it's because you're probably using resources :users in your routes

if you want to keep your show action, and use the linkedin part too, put your custom route before the resources :users

doing that, routing will match linkedin if the request is specific (users/linkedin) and if not, will continue searching and the next one will be show of course

read more about routing in general here


Routes are matched in the order they are defined. Put more specific routes higher than less-specific ones.

In your case, just make sure you define your custom route for users/linkedin before your more generic resources :users route is defined.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜