Rails redirect scoped routes - ruby-on-rails

I have an app with the following scoped routes:
scope "(:locale)", :locale => Regexp.new(I18n.available_locales.join('|')) do
# ...
# lots of routes here
# ...
end
As you can see, the 'locale' is optional here. Both http://myapp.com/en/foo and http://myapp.com/foo end up to the same controller#action. Works great.
Now I want to get rid of the locale in the url, but I want old routes still to work, so simply removing the scope statement won't do. I'd like to redirect old locale based url to be redirected to the non-locale url. Like this:
http://myapp/en/foo redirect to http://myapp/foo
and
http://myapp/foo still to work as it used to do.
So far, I only found a 'redirect' option in the Rails guides for individual routes. But I'd like this to hold for a collection of routes; the routes in my 'scope' block.
Any ideas on how to get this working?

My guess - from a lot of url stripping experiments on a different pattern - something like this would have worked for you:
scope "(:locale)", :locale => Regexp.new(I18n.available_locales.join('|')) do
get '/en/:ccc' => redirect(path: "%{ccc}")
end
The path may need a slash before the variable, and you can of course have a different language code to strip.
The big annoyance here is redirect feeds two separate classes, depending on hash or string - OptionRedirect or PathRedirect. Option redirect lets you do a lot more with variables, as of writing.

Related

Nested routes in rails

I am someone who has always liked sinatra better than rails, and has never had to do a large enough scale project that rails was required (all the sources I have read say that rails is better for larger scale projects) and now I do have to do a large scale project. I have gotten very confused with the url structure of rails. What I am trying to do is the rails equivalent of this:
get "/" do
erb :index
end
get "/home" do
erb :dashboard
end
get "/home/profile" do
erb :profile
end
get "/home/friends" do
erb :friends
end
In the first one I understand that I should put in app/routes.rb I should put root home#index and in the home controller I should put def index end.
In the second one, I understand that I should do the same except replacing index with home.
But for the third and forth ones I have no idea what to do.
Also, is the a RESTful way to do the first two?
You probably want something like this
root 'home#index'
get 'home' => 'home#dashboard'
get 'home/profile' => 'home#profile'
get 'home/friends' => 'home#friends'
remember to use the command rake routes to see all your routes, where they lead and what their names are (if they have any)
I never understood what RESTful means, so someone else will have to answer that part of your question.
K M Rakibul Islam has shown you what can be called a "resourceful" way to do routes (because it uses the keyword resources) but it looks like you're just doing the static pages at this stage, so it's not necessary.
The simplest way to do routes is with this formula:
method url => controller::action, as: route_name
where method can be get, post, patch or delete so you can have different actions linked to the same URL depending on the method the request uses.
Putting a name on the route is optional, but it gives you a clean way to use the route in your views (route_name_path)
When you start making models then you'll find that using the resources keyword comes in handy. Read about it.
You can have this:
resources :home do
collection do
get :profile
end
collection do
get :friends
end
end
end
This will give you routes like this:
profile_home_index GET /home/profile(.:format) home#profile
friends_home_index GET /home/friends(.:format) home#friends
The standard way of declaring the root path:
root 'home#index'
And for the 2nd one, you have to do:
get 'home' => 'home#dashboard'
which will give you this route:
GET /home(.:format) home#dashboard
One route can be defined in many ways that works. But, Rails has conventions that should be followed while defining routes in your Rails app.
I would highly recommend you to take a look at the Rails Routing Guide

Generate RESTful routes with dynamic prefix

I have an e-commerce web site written in Rails 3.2.8 which sells tickets for music events. So far I've been using simple RESTful routes in the application:
/ => default route: /events
/events
/events/1
/events/1/new
/events/2
...
All events used to happen in the same place, but from now on there's going to be two places, let's say "Morumbi" and "MaracanĂ£". Place is a model in the application, and it's a very important distinction between the events. So I'd like to make the place name a part of the path, and have routes like this:
/ => default route: page to choose place
/morumbi => same as /morumbi/events
/morumbi/events/1
/morumbi/events/1/new
/maracana => same as /maracana/events
/maracana/events/2
...
Although I know how to do that using the #match method, I have already a good number of routes created with the much more maintainable #resources method, and I'd like to keep them.
Do you know a solution avoiding the use of #match?
You need to use the #scope method:
scope path: ':place_name', as: 'place' do
resources :events
...
end
So GET /morumbi/events/1 will call EventsController#show with parameters id: '1' and place_name: 'morumbi'.

Rails 3 URL without controller name

Suppose I want to have a blog with Rails 3 on my website and it will be the only thing I have on it. I would like to use Rails to implement it but I don't like the URLs Rails produces. I would like URLs like this:
example.com/2012/05/10/foo
I don't want something like that which I know how to do (with to_param):
example.com/entries/2012/05/10/foo
I still want to use the helpers like
new_entry_path(#entry) # -> example.com/new
entry_path(#entry) # -> example.com/2012/05/10/foo
edit_entry_path(#entry) # -> example.com/2012/05/10/foo/edit
destroy_entry_path(#entry)
form_for(#entry)
link_to(#entry.title, #entry)
and so on. I then will have comments and want to make them accessible as their own resources too, like
example.com/2012/05/10/foo/comments/5
and those urls should also be possible to get with the normal helpers:
edit_entry_comment_path(#entry, #comment) # -> example.com/2012/05/10/foo/comments/5/edit
or something like that.
So is it possible to get URLs without the controller name and still use the url helper methods? Just overwriting to_param will always just change the part after the controller name in the url. It would be really helpful to get some example code.
Your routes.rb probably has a line something like this:
resources :entries
which produces routes of the form /entries/2012/05/10/foo.
There exists a :path argument that allows you to use something besides the default name entries. For example:
resources :entries, :path => 'my-cool-path'
will produce routes of the form /my-cool-path/2012/05/10/foo.
But, if we pass an empty string to :path, we see the behavior you're looking for:
resources :entries, :path => ''
will produce routes of the form /2012/05/10/foo.

Routes with Dash `-` Instead of Underscore `_` in Ruby on Rails

I want my urls to use dash - instead of underscore _ as word separators. For example controller/my-action instead of controller/my_action.
I'm surprised about two things:
Google et al. continue to distinguish them.
That Ruby on Rails doesn't have a simple, global configuration parameter to map - to _ in the routing. Or does it?
The best solution I've is to use :as or a named route.
My idea is to modify the Rails routing to check for that global config and change - to _ before dispatching to a controller action.
Is there a better way?
With Rails 3 and later you can do like this:
resources :user_bundles, :path => '/user-bundles'
Another option is to modify Rails, via an initializer.
I don't recommend this though, since it may break in future versions (edit: doesn't work in Rails 5).
Using :path as shown above is better.
# Using private APIs is not recommended and may break in future Rails versions.
# https://github.com/rails/rails/blob/4-1-stable/actionpack/lib/action_dispatch/routing/mapper.rb#L1012
#
# config/initializers/adjust-route-paths.rb
module ActionDispatch
module Routing
class Mapper
module Resources
class Resource
def path
#path.dasherize
end
end
end
end
end
end
You can overload controller and action names to use dashes:
# config/routes.rb
resources :my_resources, path: 'my-resources' do
collection do
get 'my-method', to: :my_method
end
end
You can test in console:
rails routes -g my_resources
my_method_my_resources GET /my-resources/my-method(.:format) my_resources#my_method
You can use named routes. It will allow using '-' as word seperators. In routes.rb,
map.name_of_route 'a-b-c', :controller => 'my_controller', :action => "my_action"
Now urls like http://my_application/a-b-c would go to specified controller and action.
Also, for creating dynamic urls
map.name_of_route 'id1-:id2-:id3', :controller => 'my_controller', :action => "my_action"
in this case 'id1, id2 & id2 would be passed as http params to the action
In you actions and views,
name_of_route_url(:id1=>val1, :id2=>val2, :id3=>val3)
would evaluate to url 'http://my_application/val1-val2-val3'.
if you use underscores in a controller and view file then just use dashes in your routes file, and it will work..
get 'blog/example-text' this is my route for this controller
def example_text
end <-- this is my controller
and example_text.html.erb is the file
and this is the actual link site.com/blog/example-text
i figured this is works for me, and it's more effective than underscores SEO wise

I want the following urls pattern, please help I'm using rails 3

I want the following urls for my UserController:
localhost/user/join
localhost/user/login
localhost/user/user_name (this can be any name in here, it should fire the 'profile' action)
Then within the /user/user_name_here/ folder I want:
/user/user_name/blah1/
/user/user_name/blah2/
/user/user_name/blah3/
It seems doing resources :user only creates things for index/show/get, so I'm confused as to how to do this other than creating so many match '/user/join' etc. lines in routes.
match "user/:user_name" => "users#show"
then /user/username will redirect to the User controller, call the show method, and pass the :user_name param
you could do the same to other actions that doesn't neet parameters,
match '/user/login' => "sessions#new"
match '/user/join' => "user#new"
Yup - 'resources :user' is just scaffolding for the usual CRUD methods. If you want paths additional to those, you're going to have to create routes (it's the only way your app knows how to route a given URL to a given controller and method).
The friendly_id gem does something similar to what you're suggesting (though I believe it's monkey-patching the .find method on ActiveRecords classes rather than handling routing specifically).

Resources