Change model name in browser rails - ruby-on-rails

I have a model in rails and I called it free_mess
And my routes file contain resources :free_mess
Obviously this model name is not ideal to show in the browser, it shows up like this:
localhost:3000/free_mess/show
localhost:3000/free_mess/index
localhost:3000/free_mess/message1
I need to change the free_mess in the browser to something more readable like 'messages'. So that the browser shows this:
localhost:3000/messages/show
localhost:3000/messages/index
localhost:3000/messages/message1

resources :free_mess, path: 'messages'
This will add alias routes in your app.
But if you want to rename the path AND the shhelper methods, then you should do:
resources :stories, :path => :books, :as => :books
See: Overriding the Named Helpers

Do this in config/routes.rb
To get this
localhost:3000/messages/show
localhost:3000/messages/index
localhost:3000/messages/message1
Do this
get 'messages/show' => 'free_mess#show'
get 'messages/index' => 'free_mess#index'
get 'messages/message1' => 'free_mess#message1'

You can specify a path option:
resources :free_mess, path: 'messages'

Related

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

Remove controller_name from URL in Rails

My Url is http://www.example.com/players/playersdetail/100006 i
have a players controller. I need to remove or hide controller and
method name (players/playersdetail) from url and i want name in place
of id(100006)
Ex- http://www.example.com/gagan-gupta
Ex- http://www.example.com/gagan-gupta/about
Ex- https://www.facebook.com/gagan-gupta/friends
How to achieve in Ruby On Rails?
Routes is
Something::Application.routes.draw do
get "players/search"
post "players/search"
get "players/playerslist"
get "players/playersdetail"
get "players/list"
get "players/followers"
get "players/following"
end
Your::Application.routes.draw do
scope ":name" do
get '', to: 'players#show'
get :about, to 'players#about'
get :friends, to 'players#friends
end
end
Reference
Your routes.rb something like this:
resources :players
which produces routes of the form /entries/24225252/foo.
There exists a :path argument that allows you to use something besides the default name entries. For example:
resources :players, :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 behaviour you're looking for:
resources :players, :path => ''
will produce routes of the form /2012/05/10/foo.
Also another option:
get ':year/:month/:day/:title' => 'some_controller#show', :as => 'players'
put ':year/:month/:day/:title/edit' => 'some_controller#edit', :as => 'edit_players'
delete ':year/:month/:day/:title' => 'some_controller#destroy', :as => 'destroy_players'

How do I change a folder url?

I have a simple question in RoR that I couldn't find a simple solution
I have a www.example.com/items folder, and I want to rename it as www.example.com/admin for every url.
Is there a way to do it in routes.rb? I tried this
this is the way I'm naming it on routes.rb:
Portal::Application.routes.draw do
resources :items do
resources :requisitos
resources :videos
end
But it didn't work.
Also, I can't just rename the folder, as there are lots of files and links that depends on it.
you set the :path parameter:
match '/items' => 'items#index', :as => 'admin', :path => '/admin'
Keep in mind the order of precedence with routes. The line above will most-likely take precedence over the changed route of the second line for the path of /admin

How can I rename a Rails controller with a route?

I have a controller in a Rails 3 app named "my_store." I would like to be able to use this controller as is, except replacing "my_store" in all the URL's with another name. I do not want to rename the controller file, and all the references to it. Is there a clean way to do this with just a routing statement?
If you use RESTful routes:
resources :another_name, :controller => "my_store"
Otherwise:
match "another_name" => "my_store"
If your routes are RESTful, this is pretty easy.
resources :photos, :controller => "images"
You can see how to do this and other helpful Rails routing information in the Rails routing guide.
Update, the other guys are correct, to replace all references you would change the resources name and corresponding controller in routes.rb! My answer is only good to set a specific route.
Yup, you would do this in your routes.rb using the :as option to specify
example:
match 'exit' => 'sessions#destroy', :as => :logout
source

Can I change my map.resources line in routes.rb to change the model name in the URL?

At the moment, I have this line in routes.rb
map.resources :usernotes
so my paths are /usernotes/new etc
But I realise now I want them to be like /notes/new
Is there a way to change the line in routes.rb to do this?
Yep, it's super-easy:
map.resources :usernotes, :as => 'notes'
See section 3.7.4 here:
http://guides.rubyonrails.org/v2.3.8/routing.html#controller-namespaces-and-routing (for Rails 2.3.x)
or here for Rails 3: http://guides.rubyonrails.org/routing.html#naming-routes
This will give you /notes and /notes/new etc
resources :notes, :controller=>"UserNotes"
You'll use new_note_path etc

Resources