Rails route redirect to a inner url - ruby-on-rails

I've changed in my rails project the url from /tours/* to /tours/peru/. It's working fine but google already indexed the /tours/ url so I want to write a route that redirect the the URL to the new version of the URL
My code It's like this:
resources :tours, path: '/tours/peru' do
resources :reviews
resources :images
resources :quotes
end
match "/tours/:id" => redirect("/tours/peru/:id")
So I'm sure who to write the redirect to make it work

I think the correct syntax is:
match "/tours/:id" => redirect("/tours/peru/%{id}")
See the documentation for more info.

If you're looking for a GET redirect, I'd propose something like:
get '/tours/:id', to: redirect('/tours/peru/%{id}')
For more ruby 1.9+ syntax. Don't mean to be too picky. :)

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

Changing the route from posts/article to blog/article in rails

I'm in trouble with routes in Rails. I've been trying to get a path like: home/blog/title-article but the closes I get is: home/blog/posts/title-article
I've tried with namespace, scope and one by one with get 'blog' => 'posts#blablabla', but I get errors like UrlGenerationError or NameError every time I change the paths. I've read the official guide and some answers in this forum, but I'm getting more confused hehe
In my last try I generated a controller Blog and a scaffold Post. The output of rake routes is: http://i.stack.imgur.com/gdfPc.png
routes.rb
Rails.application.routes.draw do
scope :blog do
resources :posts
end
get 'blog' => 'blog#index'
root 'pages#index'
...
Thank you!
Now my routes are like: http://i.stack.imgur.com/cKsFG.png
Thank you!
Not sure its what you expect but try:
resources :posts, path: 'blog'
Feels weird though.
Btw, I guess you have the errors due to url helpers.
use rake routes to check the existing routes and the related url helpers.
Doc lives here

how to change links to more SEO friendly rails

I have such links in my app
http://localhost:3000/lv/manufacturer_products?manufacturer=Komptech
http://localhost:3000/en/products?category=Shredders
But my friend said that these links are not SEO friendly, tht I have to change them, to
http://localhost:3000/en/manufacturer_products/Komptech
or similair to this
http://localhost:3000/en/products/category/Shredders
But how can I actually change the structure off link without help off any gem ? using routes ?
Thanx
See documentation for namespaces and also this answer on SO.
You could even just do named routes. something like this:
resources :products do
resources :manufacturers
end
which for the index action of manufacturers would return this:
product_manufacturers GET /products/:product_id/manufacturers(.:format) manufacturers#index
and you could then write in routes.rb
match '/:id/products/:name',
:to => 'manufacturers#index', :as => :manufacturers
and when you call it
<%= link_to #manufacturer.name, manufacturers_path({id: #manufacturer.product_id, name: #manufacturer.name}) %>
which would render http://localhost:3000/x/products/Komptech
There is a railscast by Ryan Bates for this and I always follow this,
http://railscasts.com/episodes/314-pretty-urls-with-friendlyid
I can not restrict me to share this ...Once I got an excellent help to optimize the SEO of my site
look into the link
http://complitech.net/seo-basics-high-benifit-for-ruby-on-rails-developer/
Look at the 3rd point got your answer for url
3) Improve your structure of URL
Generally in old fashion the url are unstructured and not directory wise , so make your URL are structured.
example:
www.herrybaseballcards.com/images/baseball/top-ten-baseballcards.html
so in routes
match '/:foldername/:products/:name',
:to => 'products#index', :as => :products
so ignore the Query Based URL Structures

Getting no route matches on the show action

I'm trying to use the path from the following route, here is what it's like in rake routes
chapter GET /chapters/:id(.:format) {:action=>"show", :controller=>"chapters"}
chapter_path creates a link to /chapters/x which is correct but I get the routing error when trying to access it.
No route matches {:controller=>"chapters"}`
this is my routes (I am using shallow routing to create a books_chapters and book_chapters_new paths.
resources :books do
resources :chapters, :shallow => true
end
when I test the route with rake routes, I get books_chapters, books_chapters_new, chapters and books, so I don't know what's wrong.
when i remove :shallow => true, i can access /books/1/chapters/6 but I just want it to be /chapters/6
this is what my terminal looks like
so /chapters/id and /chapters/id/edit should be working fine.
I've restarted the server with touch tmp/restart.txt and ran rails s to see if the routes worked there too and rake routes is giving me acceptable routes, but they don't work for me.
Are you supplying the parameter for the path helper, something like
chapter_path(#chapter)
I couldn't figure out how to get :shallow routes to work, and there isn't an example on how to use :shallow in the rails guide so, instead I have to just use nested routes like so
resources :books do
resources :chapters
end
now this means the something like chapters_url or chapters_path won't work.
So I have to do something like this everywhere
book_chapter_url(#chapter.book, #chapter)
or
edit_book_chapter_path(#chapter.book, #chapter)
It works but there's a bit of code smell because I use #chapter twice and the whole url should be able to resolve just through the chapter id instead.

Resources