How to make quora.com-like URL in Ruby on Rails? - ruby-on-rails

For example,
http://www.quora.com/Is-getting-ripped-worth-it
As you can see, the URL for a question does not have a controller's name. It is associated with the root URL. How can I do this?
In routes.rb, I tried the following:
match '/:id' => "questions#show"
That works, but new_question or edit_question paths still generate /questions/new(.:format) and /questions/:id/edit(.:format) as URLs.
Are there any elegant solutions for this problem?

There is nothing that prevents you from doing this:
match '/new' => "questions#new", :as => 'new_question'
match '/:id/edit' => "questions#edit", :as => 'edit_question'
but you'll have to remove :resources questions
One elegant solution is to use the friendly_id gem: https://github.com/norman/friendly_id
You can find similar ones at slugs">http://www.ruby-toolbox.com/categories/rails_permalinks_slugs

Related

Route in rails with simple regex doesn't match

I looked on the web for a while but I can't get this to work. Our application has to work with urls like ourapp.com/meandyou, where the common element is the "and" in the parameter.
I saw that it's possible to constrain urls parameters using regex, so I added the rule to routes.rb, but without success. If I try to match the same expression using the terminal, it works. Here's the complete route file:
Railroot::Application.routes.draw do
resources :couples
get "home/index"
root :to => 'home#index'
match ':url' => 'couples#show_url', :url => /and/
end
I read that Rails nests the expression within a bigger one when matching the route, so maybe I'm doing something slightly wrong even for such a simple expression.
I'm running on Ubuntu 10.04, Ruby 1.9.3, Rails 3.2.3, Passenger 3.0.13, Nginx 1.2.1.
Thanks in advance for your help!
This should be your starting point:
Railroot::Application.routes.draw do
root :to => 'home#index'
resources :couples
match ':url' => 'couples#show_url', :constraints => { :url => /and/ }
end
This may be your answer, from the rails routing docs:
:constraints takes regular expressions with the restriction that regexp anchors can’t be used. [...]
However, note that you don’t need to use anchors because all routes are anchored at the start.
So I think what you are actually matching against is not /and/ but /^and/, which would explain why it's not working.
Try being more explicit, like this:
match ':url' => 'couples#show_url', :url => /.*and/

Rails 3 path parameters not part of params

I'm trying to upgrade a Rails 2 app to Rails 3, and I'm really having problems with a route. Here's what I have in the routes.rb file
get 'profile/:login' => 'account#profile', :as => :profile
When I go to http://localhost:3000/profile/MyUsername, it does not correctly add :login to the params hash. See here:
Started GET "/profile/MyUsername?foo=bar" for 127.0.0.1 at Tue Mar 20 21:39:03 -0400 2012
Processing by AccountController#profile as HTML
Parameters: {"foo"=>"bar"}
For some reason, :login is not part of the regular params. On a hunch, I inspected the request.env and found this:
action_dispatch.request.path_parameters"=>{:action=>"profile", :controller=>"account", :login=>"MyUsername"}
I'm totally stumped at this point. Am I missing something? Where should I look next to figure out what is going on here?
Update
I started playing with removing gems and this magically worked. I just commented out gems from the Gemfile until I got the absolute minimal set needed to load the homepage. At that point, the params were exactly as expected. Then I added gems back a few at a time in order to find the cause. I added everything back and...it works now. Crazy, but whatever it takes, I guess.
Looks like you mixed the syntax for 'match' with 'get'. Please try:
match 'profile/:login' => 'account#profile', :as => :profile, :via => :get
Or
get 'profile/:login', :to => 'account#profile', :as => :profile
in your config/routes.rb
When I want to use URL params I always use resource(s) when defining the route. That is convention with Rails 3.x so you can try it.
resources :accounts , :exclude => :all do
member do
get :profile
end
end
This should help or any other way of defining resource URL.
Something like this should work
match 'profile(/:login)' => "account#profile", :as => :profile
If it does not, there may be something else in your routes file that conflicts. Make sure any match ':controller(/:action(/:id(.:format)))' (or similar "match everything" routes) are at the very bottom of your routes file.

rails sunspot gem friendly SEO url's?

How does one get SEO friendly url's in sunspot?,
The method of search form is GET as suggested from the rails docs, but now i have a very long query string that looks terrible, is it possible to have it something like
/search/param1/bla/param2/bla
instead of the long
?search&param1=somevalue&param2=someval2
You could modify your search route to include the params. Something like this within your routes.rb:
match '/search/:param1/:param2' => 'search_controller#search_action', :as => :search_with_params, :via => :get
Then a user would visit:
/search/value1/value2
Which gives you params[:param1] and params[:param2] to access the values from the url.
If those 2 params are not required for all searches I believe you would also need a route just for the search action:
match '/search' => 'search_controller#search_aciton', :as => :search, :via => :get

Make routes upperscore instead of underscore?

Very basic and maybe I missed it, but instead of doing something like sign_up for my routes, how can I do sign-up? This is assuming I have these route like this:
get "/sign_up" => "devise/registrations#new"
It doesn't work for me if I change it to: get "/sign-up".
Try this instead:
match "/sign-up" => "devise/registrations#new", :as => :sign_up
Unfortunately ActionDispatch works best with underscores. So you might try compromising by keeping with convention for the sign_up_path helper and using the dash in the URL

Question about routes.rb

Rails newbie here.
Can anyone please explain the difference to me between the following lines of code:
match '/' => 'posts#index'
and
match '/' => 'posts#index', :as => 'posts'
The reason I'm asking is because when I use the latter code, I cannot create new posts :|
The latter is creating a named route. It creates a helper that you can call from your views, in this case, posts_path & posts_url.
That being said, I'm not sure how you are able to create new posts with either of those as you are not defining the posts#new or posts#create. Is there more to your routes file than these? Also, I'm not sure if it's a requirement or not, but you should pass your :as option as a symbol, so :as => :posts.
For reference, you can run rake routes from console and see a list of all the routes that are defined in your application. You'll also see how they are named—that's the column all the way to the right—which you can then append _path or _url to.

Resources