Removing controller name from Rails URL route - ruby-on-rails

This is my first Rails project, I am trying to piece things together slowly.
When I'm trying to view the page I generated using rails g controller <controller> <page>, I find myself going to 0.0.0.0:3000/controller/page.html, How can I configure it so that my route file globally allows viewing the page via the page name, rather than controller/page, if no such way exists, then how can I route controller/page.html to /page.html
I've looked around, and haven't really found any explanation, maybe I'm looking in the wrong places?

In config/routes.rb:
get '/page' => 'controller#action'
If your controller is:
class UsersController < ApplicationController
def something
end
end
Then config/routes.rb would be:
get '/page' => 'users#something'
For static pages you could want to use public folder though, everything you put there is directly accessible, for example public/qqqqqq.html would be accessed in localhost:3000/qqqqqq.html

We've just achieved this by using the path argument in resources method:
#config/routes.rb
resources :controller, path: ""
For you specifically, you'll want to make something like this:
#config/routes.rb
resources :static_pages, path: "", only: [:index]
get :page
get :other_page
end
#app/controllers/your_controller.rb
def page
end
def other_page
end
This will give you routes without the controller name. You'll have to define this at the end of your routes (so other paths come first)
Obviously this will form part of a wider routes file, so if it doesn't work straight up, we can refactor!

It sounds like this is a static page, so you can do as juanpastas says, or another option is to create a folder under your app/views directory to hold these pages. Maybe something like
app/views/static_pages/the_page.html.erb
Then in your config/routes.rb you can add:
match '/your_page_name', to: 'static_pages#the_page', via: :get

Related

How to handle a Rails redirect that starts with "x" pattern in the url

I am new to Ruby and Rails. So far I've successfully:
updated a url from /dashboard to /specific_dashboard
gotten the redirect working for when you put in /dashboard, it now takes you to /specific_dashboard
The problem I am running into is redirecting when there are additional params in the url like /dashboard/program... or /dashboard/dashboard_page...
Is there Ruby syntax that would be equivalent to using a wild card like get "/dashboard/*" => redirect("/specific_dashboard"). I've tried this and match '/*dashboard' => redirect("/specific_dashboard"), via: :all and can't get it to work. If its helpful, I have this code in my routes.rb file.
you can use nested routes.
Example you have authors and books.
resources :authors do
resources :books
end
https://guides.rubyonrails.org/routing.html#nested-resources
It should be the following in routes.rb:
get 'dashboard/*section', to: '<controllerName>#<actionName>'
Here:
controllerName = Controller Name, e.g. Home
actionName = Action Name, e.g. Dashboard
class HomeController
def dashboard
# inside params[:section] you'll get 'dashboard_page'
# if you're accessing '/dashboard/dashboard_page'
end
end
Reference: https://guides.rubyonrails.org/routing.html#route-globbing-and-wildcard-segments

Including attributes in custom Rails routes

I hope the title is not to misleading, as I don't know a better title for the problem I'm working on:
I have a doctor which belongs to location and specialty. I'd like to route to show action of the doc controller like this:
/dentist/berlin/7
I defined my routes like this:
get ':specialty/:location/:id', to: 'docs#show'
And in my views create the following url to link to the show action of the doc controller:
<%= link_to doc.name, "#{doc.specialty.name}/#{doc.location.name}/#{doc.id}" %>
Is this a good solution to the problem? If not, is there a cleaner way to construct urls like this possibly using resources? What the heck is the name for a this problem?
Thank your very much for your help in advance.
For references, you should have a look at this page (especially the end of section 2.6)
If it is only for a single route, it's okay as you did. But then if you want to have more than one route (like /dentist/berlin/7, /dentist/berlin/7/make_appointment, etc.) you might want to structure a bit more your routes so as to take advantage of rails resources.
For example, instead of
get ':specialty/:location/:id', to: 'doctors#show'
get ':specialty/:location/:id/appointment', to: 'doctors#new_appointment'
post ':specialty/:location/:id/appointment', to: 'doctors#post_appointment'
You could have something like this (the code is almost equivalent, see explanation below)
resources :doctors, path: '/:specialty/:location', only: [:show] do
member do
get 'new_appointment'
post 'create_appointment'
end
end
Explanation
resources will generate the RESTful routes (index, show, edit, new, create, destroy) for the specified controller (doctors_controller I assume)
The 'only' means you don't want to add all the RESTful routes, just the ones specified
Then you want to add member actions, ie. actions that can be executed on a particular item of the collection. You can chose different syntaxes
resources :doctors do
member do
# Everything here will have the prefix /:id so the action applies to a particular item
end
end
# OR
resources :doctors do
get 'new_appointement', on: :member
end
By default, the controller action is the same as the path name you give, but you can also override it
member do
get 'appointment', action: 'new_appointment'
post 'appointment', action: 'post_appointment'
end
Rails has some wonderful helpers when it comes to routing !
The correct approach is to give your route a name, like this:
get ':specialty/:location/:id', to: 'docs#show', as: 'docs_show'
Then you can use it like this:
<%= link_to doc.name, docs_show_path(doc.specialty.name, doc.location.name, doc.id) %>
Note 1:
Rails appends _path at the end of the route names you define.
Note 2:
You can see all the available named routes by executing rake routes.

How to display a friendly name in URL in rails

This is my new resource in routes file :
resources :staticpage do
collection do
get :aboutus
get :snacks
get :contactus
end
end
this generates my urls localhost:3000/staticpage/aboutus localhost:3000/staticpage/snacks/ localhost:3000/staticpage/contactus
Instead of this complex url i want to have localhost:3000/aboutus , localhost:3000/snacks/ localhost:3000/contactus in the URL.
using gem can change my action name, but i want to alter "controllername/action" to a simple string.
What modification should be done in the routes path declaration ?
You can simply write matchers for each route like this:
get 'aboutus', to: 'staticpage#aboutus'
get 'snacks', to: 'staticpage#snacks'
get 'contactus', to: 'staticpage#contactus'
Note that you don't need the resources or collection block if you do this.
As per the routes documentation, you will need to define the routes manually (like David Underwood's answer). However, there is an even better way to do it:
#config/routes.rb
static = %w(aboutus snacks contactus)
static.each do |page|
get page.to_sym, to: "staticpage#show", page: page
end
#app/controllers/staticpages_controller.rb
def show
page = params[:page]
render "staticpages#{page}"
end

Understanding routing with rails

I am trying to make a stupid simple CMS for one of my clients using rails. I generated scaffolding for Page and have been successful at creating and rendering those pages, however, the routing is ugly.
Right now, if I want to view the home page, I get a url like this: example.com/pages/1
I'd like to accomplish 2 things:
How do I set the routing so that example.com automagically grabs the page named "home"
and
How do I set the routing so that example.com/page/page_name performs a
#page = Page.find_by name: 'page_name'
Q1:
How do I set the routing so that example.com automagically grabs the page named "home"
In `routes.rb:
root :to => '[controller]#[action]'
#'pages#home' for example, if your home page is in `pages_controller`
# and uses the `home` action
Q2:
How do I set the routing so that example.com/page/page_name performs a
#page = Page.find_by name: 'page_name'
match '/page/:name', :to => 'pages#some_name', :as => :some_name
this would generate the following in $rake routes:
some_name /page/:name(.:format) pages#some_name
when you link to (or redirect, or otherwise access) this page, you'd write
link_to "click this link!", some_name_path(#SomeModel.some_name)
To accomplish the first thing you need to open the file routes.rb which is in the config folder and add the following:
root to: "home#index"
I'm assuming you have a controller named home which contains a method called index and this is the one that displays the home page.
If you want to make it the same as example.com/pages then you would have to use
root to: "pages#index"
to make a general rule you need to use
root to: "controller#method"
Don't forget to remove the index.html from the public folder too.
I recommend you make the blog application presented here:
http://guides.rubyonrails.org/getting_started.html
It can help you understand more.
Here's a solution that assumes your controller is named pages_controller instead of page_controller.
Add these routes in config/routes.rb:
root to: 'pages#show', page_name: 'home'
get 'pages/:page_name', to: 'pages#show'
For the controller app/controllers/pages_controller.rb:
class PagesController < ApplicationController
def show
#page = Page.find_by(name: params[:page_name])
end
end
Note: In rails4 the find_by dynamic finders have been deprecated... so if you're app is on 4 you should look into updating them. These docs have further details.
Also, if you're just trying to get static looking urls then I would definitely go with Marian Theisen's suggestion and use friendly_id.

Custom urls and paths in rails

I have two models: Books and pages in a typical one_to_many relationship.
How can I make the following
page_path(#page)
output this path:
bookname/page/pageid
instead of
page/pageid
If I override the to_param, all I can do is make a path like localhost/page/bookid/pageid but that's not what I want.
Not sure if it's possible to get exactly what you want, but you can make the Page a nested resource under book like this:
resources :books do
resources :pages
end
Then you will get:
localhost/book/bookid/page/pageid
Then you can override `to_param' to get:
localhost/book/bookid-bookname/page/pageid
I'm assuming you mean to have path as /:book_name/page/:id
In routes.rb:
match '/:book_name/page/:id' => "page#show", :as => :page
In the controller you would access params[:id] to get page.id and params[:book_name] to get the name of the book.
I discovered that to have full control over path helpers, you have to override those inside the application_helper.erb file. The following code worked for me:
def pages_path(#page)
#bookpath = Book.find(#page.book_id)
#bookpath + '/page/' + #page.id
end
The helper only creates the path. You still need to link it to a particular action in routes.rb. You may even nest the pages resource inside the books resource. The only important thing is that the path generated by the above helper must be recognizable by the rails application.

Resources