Custom route doesn't use :id - ruby-on-rails

I've created a custom route in my routes.rb to clone my activities by the activity ID as following:
resources :activities
get "/activities/:id/clone" => "activities#clone", :as => :clone_activity
post "/activities/:id/clone" => "activities#clone"
When I use <%= clone_activity_url(#activity) %> now; It shows me http://localhost:3000/activities//clone. instead of the ID attached.
Why isn't my ID showing?

You need to wrap the routes inside a block like this
resources :activities do
get "clone" => "activities#clone", :as => :clone_activity
post "clone" => "activities#clone"
end
This will make it so that a GET or POST to the path /activities/:activity_id/clone will trigger the method at activities#clone
Furthermore, the route will be called "activity_clone_activity_path" (or url) - you can change this in your code if you want, also this will only reference the get path since you only attached the :as to the get path, attach to both if you need.

Related

Cant rename rails routes

I want to rename a few of my routes, for example:
get 'legal/terms_of_service', :to => 'legal#terms_of_service', :as => :datenschutz
that works, but it doesn't change the acutal URI- and I want that to be changed as well. path: does not work here.
Thank you
If you want the URI to be /datenshutz then you can do this:
get '/datenschutz', :to => 'legal#terms_of_service', :as => :datenschutz
The get '/datenschutz' determines the url browsers or other http clients use to access the controller.
The :to => 'legal#terms_of_service' specifies a controller class and controller action used to respond to the route.
The :as => :datenschutz changes the method you use in views to create links to the route (such as datenschutz_path).

Rails same incoming route but different controller actions?

A user clicks on a tag like example.com/pizza, and sees all the posts with the tag of pizza in the posts controller. I want people to also see all the "alternateposts" with the tag of pizza as well, but in a different controller.
rails routes throws an error saying its already in use. what's the best way to go about this?
routes.rb
# TAGS
get 'tags/:tag', to: 'posts#index', as: :tag
get 'tags/:tag', to: 'alternateposts#index', as: :tag
You can't declare multiple route with same URL.
In your case, the seconde URL will overload your first one.
You have to declare a single route with a single controller and return posts and alternateposts in the same way.
Yes #Antoine Dewaele is right. you don't declare multiple routes with same URL.
your route file is like this
get 'tags/:tag' => 'posts#index', :as => :tag
your route file should be like this
get 'tags/:tag' => 'posts#index', :as => :tag
get 'all_pizza' => 'all_pizza#index', :as => :all_pizza
For more info you can visit here Rails Routing from the Outside In

Change model name in browser 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'

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'

Getting Routing Error(Route doesn't match)

I am trying to update multi-select list on change but I am getting a routing error.
I call this with an onchange event $.post("/levels/category_lists_for_level"
I have an action called category_lists_for_level in a controller called level.
My routes file looks like this.
match '/levels/category_lists_for_level/:id' => 'levels#category_lists_for_level'
resources :levels
resources :levels , :collection => {:category_lists_for_level => :get}
What am I doing wrong here? I never had any problem in Rails 2, all I used to add the collection.
It is a bit hard to say exactly what you need since as others have said you are missing some info, but you have a few apparent things going on here:
You are duplicating routes
You have the route set on a collection and a member
You are allowing multiple requests types (get and post) to access this route.
If you would like to have this operate on a collection you just need:
resources :levels do
post "category_lists_for_level", :on => :collection
end
or on a member:
resources :level do
get "category_lists_for_level", :on => :member
end
This will reduce your routes. Just use rake routes | grep level to get the routes for this controller.
Take a look at this for some more info.

Resources