I have moved a blog from my main domain to a sub domain and want to redirect traffic that was directed at the original blog to the same post on the subdomain using Rails 5.1 routes and wildcards.
For example, my original address was https://www.dolgins.com/blog/2018/5/22/unique-hand-crafted-mosaic-jewelry-at-richard-dolgin-private-jeweler. Now, if someone enters this address, he/she would be redirected to https://blog.dolgins.com/blog/2018/5/22/unique-hand-crafted-mosaic-jewelry-at-richard-dolgin-private-jeweler.
Here is my current routes that pertain:
Rails.application.routes.draw do
get 'blog/*all' => redirect(subdomain: 'blog', path: '%{all}')
get "/blog" => redirect("https://blog.dolgins.com")
end
However, these routes redirect to http://blog.dolgins.com/2018/5/22/unique-hand-crafted-mosaic-jewelry-at-richard-dolgin-private-jeweler versus http://blog.dolgins.com/blog/2018/5/22/unique-hand-crafted-mosaic-jewelry-at-richard-dolgin-private-jeweler with the bold showing the difference.
Any ideas about what my routes should be?
Thank you!
You were really close
Rails.application.routes.draw do
get 'blog/*all' => redirect(subdomain: 'blog', path: '/blog/%{all}')
get "/blog" => redirect("https://blog.dolgins.com")
end
Related
So this is the path for all my users:
http://localhost:3000/users
I would like to change /users to /members , so I did this:
get '/members' => 'users#index', as: "members"
This way, people can visit http://localhost:3000/members and get all the same content in /users .
But the problem is that http://localhost:3000/users is still accessible to people. How can I remove/hide/redirect /users, so that people will see /members when they try to use this old /users url: http://localhost:3000/users ?
Well, it depends on if you want to eliminate the path or simply override it
Since you mention that http://localhost:3000/users still maps somewhere, I'm going to assume that you have either the line get '/users' => 'users#index' or more likely something like resources :users. If you have the first line, simply delete it, if it's the second you can restrict the routes that you create with the :except option
resources :users, except: :index
For a redirect, the solution hamdi posted should work as well.
Check out rails routings page for more info: http://guides.rubyonrails.org/routing.html
Cheers
You can redirect like the below:
get '/users', to: redirect('/members')
Also you can find more info for redirection at the below link:
http://guides.rubyonrails.org/routing.html#redirection
There seems to be something wrong with my routing paths. Normally I should be able to do something like <%= link_to Profile, user_path(#user||current_user) %> and I move on with my day. For whatever reason I'm failing to understand my user_path is not returning /user/:id like I would expect it to do. Instead it is returning /user.:id
To test this, I loaded a partial with the following code.
app/view/users/_test.html.rb
<%= #user %><br>
<%= #user.id %><br>
<%= link_to user_path(#user), user_path(#user) %><br>
<%= new_user_path %><br>
<%= edit_user_path(#user) %><br>
<%= url_for(#user) %>
This returned
localhost:3000/test
#<User:0x007fb6cd341f08>
1
/user.1
/users/new
/users/1/edit
/user.1
I can't figure out what is causing this to happen. The edit_user_path(#user) works perfectly, but the show doesn't. I have read the Rails Routing Guide from top to bottom about three times and I can't figure it out. The closest I found to my problem was on an old Rails 3.1 gem problem with Devise, but I'm not even using the Devise gem (maybe I should be?).
Why is my route failing? I'm not really looking for a workaround (though I suppose I'd rather have a workaround than no solution), I want to understand why this is happening and fix it. What's going on?
My routes are pretty vanilla, nothing special going on there, but just in case the problem is there and I missed it, here it is.
config/routes.rb
Rails.application.routes.draw do
root 'static#home'
%w( 404 406 422 500 503 ).each do |code|
get code, :to => "errors#show", :code => code
end
#USER PAGES
get '/test' => 'users#test'
get '/signup' => 'users#new'
post '/user' => 'users#create'
get '/user/list' => 'users#index'
post '/user/' => 'users#update'
get '/user/:id' => 'users#show'
get 'profile', to: 'users#show'
resources :users
end
I'd suggest updating your routes as follows:
config/routes.rb
Rails.application.routes.draw do
root 'static#home'
%w( 404 406 422 500 503 ).each do |code|
get code, :to => "errors#show", :code => code
end
#USER PAGES
get '/test' => 'users#test'
get 'profile', to: 'users#show', as: :user_profile
resources :users
end
...as resources :users creates all of the routes I've removed.
The duplicates may have been overwriting a default path, causing the behaviour you're seeing - if you pass an object to a url helper that doesn't expect a parameter, you see the behaviour you're getting (i.e. doesnt_have_an_id_path(#object) => /doesnt_have_an_id.1).
I've also added a name to the user profile path to avoid this clashing. See if this works (perhaps one step at a time to get the cause and effect) - otherwise, try temporarily removing the profile route.
Re that path, you may have a problem in that the users#show action will expect an :id parameter, yet that route doesn't allow for one. This might be causing the current problem, though if not, may cause issues down the line.
Hope that fixes it - shout if you've any questions / feedback when you've tried it out.
In your case the two conflicting routes are:
post '/user/' => 'users#update'
get '/user/:id' => 'users#show'
You are trying to use user_path helper method, thus you need to have user named route.
If you run rake routes for these two routes you will see this:
Prefix Verb URI Pattern Controller#Action
user POST /user(.:format) users#create
GET /user/:id(.:format) users#show
The Prefix column shows you the named routes which are defined for your application. In this case Rails auto-generated named route user for POST /user endpoint. That means user_path will return /user instead of /user/:id as you expected. Rails generate routes like these when it sees a route definition without parameters. For example if you have get /user/some/more in your routes, Rails will auto-generate named route user_some_more for you and they you will be able to use user_some_more_path helper.
In order to fix your particular case you can stop rails from generating route for POST endpoint by doing this: post '/user/', as: nil and give GET endpoint a name you want: get '/user/:id', as: 'user'. Then you will be able to use user_path(user) to generate paths of format /user/:id.
using
Rails 5.1.4 &
ruby 2.4.1
Wondering why Rails would not give a destroy_visitor_path for DELETE request. Any info would be greatly appreciated. Thank you!
routes.rb
Rails.application.routes.draw do
root :to => 'contents#index'
resources :contents
resources :visitors
end
Routes:
visitors GET /visitors(.:format) visitors#index
POST /visitors(.:format) visitors#create
new_visitor GET /visitors/new(.:format) visitors#new
edit_visitor GET /visitors/:id/edit(.:format) visitors#edit
visitor GET /visitors/:id(.:format) visitors#show
PATCH /visitors/:id(.:format) visitors#update
PUT /visitors/:id(.:format) visitors#update
DELETE /visitors/:id(.:format) visitors#destroy
Same reason why there's no update_visitor_path — it's a singular resource that simply responds to a different HTTP verb.
In your view, you'd link_to "Delete", visitor_path, method: :delete
I can't comment on the philosophy behind the design decision, or any of the arguments about whatever spec it's designed to because that's for people who are smarter than I am.
More info on the Rails routing guide: http://guides.rubyonrails.org/routing.html
There are many incidents to delete
It's let you set your own routes:
For example you want to logout after delete (usually in sessions)
get '/logout' => 'sessions#destroy', :as => 'logout'
I have a really simple app I've built using RoR but I'm stuck modifying my routes.
It's basically a site which lists user information - I need to change the url from:
mydomain.com/users/user-1
to
mydomain.com/user-1
Update..
I've managed to route the above request using:
match "/:id", :controller=>"users", :action=>"show"
But what I really need to do is change the route for all requests to /users/# to /
Although my route is working, all my links to show a user still point to:
/users/user-#
--- Update ---
The routing for /user-id is now working perfectly however, I'm struggling with the rest of the routing now.
I can now navigate to http://localhost/user-1
However, I basically need to remove the /user/ part completely. When I'm editing / updating a page, I end up with it going to:
/users/user-1/edit
All works fine but it then redirects to"
/users/user-1/
I really need both of these to redirect to
http://localhost/user-1/edit
Thanks
Bob
You want:
resources :users, :path => '/'
I believe get ":id" => "users#show" will be much the same except you only allow HTTP GET. Hope this works.
At the bottom of your routes
match "/:id", :to => "users#show"
There is some side effects so be ready
to rewrite your routes you should specify its name:
match "/:id", :to => "users#show", :as => :user
or, as #Whirlwin pointed, better to use just GET request as default
get "/:id", :to => "users#show", :as => :user
So now you can call:
user_path(#user)
routes:
match '/' => 'site_admin/admin#index'
resources :link_pages
resources :services
resource :user_session
resource :account, :controller => "users"
resources :password_resets
resources :users
resources :addresses
resources :info
match "/home", :to => 'info#home'
match "/register", :to => 'users#new'
root :to => 'info#home'
match ':controller(/:action(/:id(.:format)))'
so when I got to admin.lvh.me:3000/ it goes to site_admin/admin#index... which is great...
but when I take off the subdomain, and just have lvh.me:3000/ it goes to the same route....
how do I get admin to stay where it is. and no subdomain to go to my root page, as in my routes file?
Routes are parsed in order, so when you request / from any domain it finds "match '/'..." first and sends you to the specified page. Your subdomain isn't coming into play at all. You can use Request-based constraints to route based on subdomain:
http://guides.rubyonrails.org/routing.html#request-based-constraints
Not sure how subdomain factors into this at all. Perhaps you're confusing subdomain with route namespacing (http://edgeguides.rubyonrails.org/routing.html#controller-namespaces-and-routing)?
match '/' => 'site_admin/admin#index'
Is being selected over
root :to => 'info#home'
Because it's defined first in the routes file. They're ostensibly the same thing.
Yes #Cory is right. Above both statements are similar and first defined route is considered every time. If you change admin route to
match '/admin' => 'site_admin/admin#index'
then it does make sense... What say??
or else, using the following code you can determine your URL conditionally:
request.subdomains(0).first will give you the subdomain value- either admin or blank. But it will go to any one controller action only which is defined first in route.rb file.
Then from that action using subdomain, you can decide where to re-direct it- either to admin panel or home page...