Remove underscores from page links in browsing bar - ruby-on-rails

I came up with problem that to make good SEO there can't be underscores in links. I have project runing Ruby On Rails 3.
So I now wonder how it is posible to remove them and/or replace them with dashes.
I tried to search for this Q but there apeared just answers that are related to CSS. Like removing underscore from links.
Here is part of my routes file.
root :to => "home#index"
devise_for :admin_users, ActiveAdmin::Devise.config
ActiveAdmin.routes(self)
scope "(:locale)", :locale => /en|lv|ru/ do
resources :products,:waste_equipment,:sitemap,:energy, :energy_wood_machinery,:fuel_tanks, :soil_equipment,:soil, :tractors_and_loaders,:support_for_sport,:search,:location,:used_items,:admin_users,:conditions, :details,:projects,:knives,:messages, :mails, :photos,:emilianaserbatoi_products, :send_email_us,:subscribes, :home,:manufacturers, :location, :categories, :news, :manufacturer_products, :about_us,:contacts, :services, :partners
end
I wonder, that can be done using routes file or some gem is necessary?
Thanks!

You can do what you suggest in your comment, but you must not do it at once because you 'll end up with all your previous link invalidated and that is the SEO equivalent of shooting your leg.
you should:
add the new rules as an extra set of rules
log legacy link access
provide moved_permanently status code (301) for redirecting from old routes to new
a while after your logs are empty from legacy links remove them from google admin tools and update the routes

Related

Ruby Rails - Routing without Resource Name

I'm currently using friendly_id and I'm trying to get route to not include the resource name.
Normally, the URL is www.website.com/projects/25.
With friendly_id it's www.website.com/projects/fun-project. This is what I currently have.
And I'd finally like it to be www.website.com/fun-project.
I found this question about this but the solution wasn't working for me for one case.
//routes.rb
root 'static#index'
...
resources :projects, path: ''
When doing resources :projects, path: '', the projects#show works correctly, but I cannot use projects#index as I was before.
In my navigation, the projects_path that used to link to www.website.com/projects now just links to www.website.com.
Do I need to do something else for this case?
Thanks.
You can use simply get for this purpose in the following way:
get '/:project_id' => 'projects#show'
What I understand so far, you want the following URL to your projects resources.
www.website.com/fun-project
here 'fun-project' is the friendly id of your project.
In order to achieve this you can add the following line to your route:
get '/:id' => 'projects#show'
And should work!

Redirection in Rails 4 routes

My site used to have a mobile view here:
https://www.example.com/m/home
We have deprecated the mobile views and now I need a simple way to trim the /m/ off the URL so that the request proceeds to the correct page.
Example:
https://www.example.com/m/about => https://www.example.com/about
https://www.example.com/m/user/:id => https://www.example.com/user/:id
I'm hoping to solve this in the Rails routing without having to introduce a new controller action or meddle with nginx. I have 100+ routes. Thanks in advance.
Rails version: 4.2
There is a redirection module (also documented in the guide).
Something like :
get '/m/about', to: redirect('/about')
get '/m/user/:id', to: redirect('/user/%{id}')
Which you can combine with route globbing for a generic solution :
get '/m/*path', to: redirect('/%{path}')
How about just refactor your routes a bit:
Eg: Previous routes.rb
resources :users
# ...
Now, it becomes:
['m', ''].each do |sc|
scope sc do
resources :users
# ...
end
end

Is it possible to have same routes with different show action in rails?

I am trying to reach the following url in my rails app:
example.com/user12 # It should show user#show
exmple.com/wordpress # It should show category#show
My solution: (it does not work)
In the routes.rb I have added :path => '' to both categories and users in order to remove the controllers' name from the url.
resources :categories, :path => ''
resources :users, :path => ''
When I run rake routes, I have the following urls"
category GET /:id(.:format) category#show
account GET /:id(.:format) accounts#show
So I assumed that It should be working correctly, but surely not. It only works for category names and for usernames it shows ActiveRecord::RecordNotFound.
I know, my solution is wrong because I am confusing rails route system and because the resources :categories has more priority over resources :users, the category show page works fine.
So Is there any solution to solve an issue like this?
I have finally found the solution with constraints option. This option accepts regular expressions.
resources :categories, :path => '', constraints: { id: /wordpress|php/ }
Every category should be added manually in this way OR (I am not sure) maybe there is a way to list all categories from database automatically.
One way of doing that is to override the default rails routes, to do that remove the resources
I tested and this works
SampleRails4::Application.routes.draw do
get '/user12', to: 'users#show' #=> users/show
get '/wordpress', to: 'category#show' #=> assuming u have a controller and action
end
however then you have to update the rest of your code, Ex: users/show might be expecting the user id as a param read more about rails routing

Rails: simplest way to put a blog on a subdomain

I've been digging around on subdomains in Rails for a couple days and haven't found a good explanation of this yet...
I have a rails app which has a blog integrated into it, and I'd like to put that blog on a subdomain. Ie. blog.myapp.com.
Now, within the blog I want the user to be able to view posts, blog.myapp.com/posts/123. However, if the user were to click to any other resources on the site, say videos for example, I'd like them to be redirected back to root, ie. www.myapp.com/videos/123. I don't want blog.myapp.com/videos... to cause a routing error, I just want it to redirect.
Basically, I'm looking for the simplest way to setup a subdomain and specify that certain controllers use that subdomain and the others don't. Ideally I'd even like the controller layer to handle redirection both ways, so that in views I could link back and forth to things just using helpers like post_path(123) and video_path(123) and that the subdomain would automatically be used or not used based on which controller was serving the view.
I tried putting all the controllers in a constraints block, ie:
constraints :subdomain => 'www' do
resources :sessions
resources :users
resources :videos
root :to => 'home#show'
end
constraints :subdomain => 'nexturb' do
resources :posts
root :to => "posts#index"
end
root :to => 'home#show'
However this doesn't seem to work well, I've especially had trouble with getting redirection between links to work very consistently.
I'm pretty sure other people must have run into this issue in the past, but I can't seem to find a good example of this situation in writing. What's the best way to handle this?
With help from here, here, and here... I finally figured this out:
constraints :subdomain => 'blog' do
scope '', :subdomain => 'blog' do
resources :posts
end
root :to => 'posts#index'
end
This causes the posts_path helper to correctly send visitors to the subdomain.
I used #Andrew's answer. Some extra tips:
This railscast suggests using lvh.me to make subdomains work on localhost by visiting urls like blog.lvh.me:3000 or lvh.me:3000
Add the routes available in Andrew's answer
You may then have to add this to config/development.rb:
config.hosts += ["lvh.me", "blog.lvh.me"]

Do you have to mess with Rails's "routes.rb" file?

I never touch routes.rb beyond calling map.root to set a default route. I've always been content to use URLs of the form...
/controller/action/perhaps_an_id
and it works fine.
Does this make me a bad person? Am I missing out on something totally awesome?
What if I try to adopt RESTful design? Would that mean I have to edit routes.rb or could I continue to pleasantly ignore it?
(I tried to read up on this topic in The Rails Way but it was unbearable.)
If you generate your resources with the default scaffolding then it will even include the restful routing for you in routes.rb.
If you're not using the scaffolding then the reason that it's working is because of the default routes at the bottom by default:
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
I've been following that it's best practice to remove these for production applications and instead make sure that only the resources that need to be exposed are exposed. With Rails 2.2 you can even limit the RESTful methods from map.resources by:
map.resources :posts, :only => [:index, :show]
map.resources :comments, :except => [:edit]
There's also tons of cool things you can do with nested resources, named routes, etc. They have a lot of examples in the docs (http://www.railsbrain.com/api/rails-2.2.2/doc/index.html?a=M000255&name=resources)
You may also want to make custom named routes for your marketing department (eg: mycoolsite.com/free-trial) that go off to specific controllers and actions, etc.
Ryan Bates has a series of screencasts that go over some of the neat things you can do with routes: http://railscasts.com/tags/14
Not having switched to RESTful design does not make you a bad person and if you feel no need to change keep writing your apps the 1.x way.
The majority of Rails developers has adopted REST and seems to be very happy about it. I don't think there is a need here to repeat all pro REST arguments.
You do need to add one line per resource to your routes file such as:
map.resources :posts
If you were to go RESTful, yes you would have to edit routes.rb and add your resources like,
map.resources :your_resource
or if you have nested resources,
map.resources :people do |person|
person.resources :ideas do |idea|
ideas.resources :bad_ones
end
end

Resources