Dave
Dave

Reputation: 19090

How do I build a dynamic route in Rails 5?

I'm using Rails 5. I would like to construct a URL of the form

/my_objects/city_code

and I would like it to link to my controller, my_objects_controller.rb, specifically this method

  def search_by_code
     ...
  end

Then in this method, I would like to be able to extract "city_code" as a variable. I don't know routes well enough to figure out how to do this. How can I pull this off?

Edit: I tried adding your route to the existing block I already had ...

  resources :my_objects do
    collection do
      post 'create'
      get 'import'
      get '/index', to: redirect('/races/index')
      get 'search'
      get 'stats'
      get ':code', to: '#search_by_code'
    end
  end

but it resulted in the below error when I started my app ...

/Users/davea/.rvm/gems/ruby-2.4.0/gems/actionpack-5.0.3/lib/action_dispatch/routing/mapper.rb:314:in `block (2 levels) in check_controller_and_action': '' is not a supported controller name. This can lead to potential routing problems. See http://guides.rubyonrails.org/routing.html#specifying-a-controller-to-use (ArgumentError)

Upvotes: 0

Views: 1754

Answers (1)

vitulicny
vitulicny

Reputation: 321

You have to have in your config/routes.rb:

get '/my_objects/:code', to: 'my_objects#show'

Then in your my_objects_controller.rb in the show action pass the params[:code] to the model:

MyObject.search_by_code(params[:code])

In the my_object.rb model:

def self.search_by_code(code)
  MyObject.where(code: code)
end

FIX ACCORDING TO QUESTION EDIT:

Fixed routes.rb:

resources :my_objects do
    collection do
      post 'create'
      get 'import'
      get '/index', to: redirect('/races/index')
      get 'search'
      get 'stats'
      get ':code', to: 'my_objects#search_by_code'
    end
  end

Run this from your terminal:

rake routes

and it will output:

     Prefix Verb   URI Pattern                    Controller#Action
       my_objects POST   /my_objects/create(.:format)   my_objects#create
import_my_objects GET    /my_objects/import(.:format)   my_objects#import
 index_my_objects GET    /my_objects/index(.:format)    redirect(301, /races/index)
search_my_objects GET    /my_objects/search(.:format)   my_objects#search
 stats_my_objects GET    /my_objects/stats(.:format)    my_objects#stats
                  GET    /my_objects/:code(.:format)    my_objects#search_by_code
                  GET    /my_objects(.:format)          my_objects#index
                  POST   /my_objects(.:format)          my_objects#create
    new_my_object GET    /my_objects/new(.:format)      my_objects#new
   edit_my_object GET    /my_objects/:id/edit(.:format) my_objects#edit
        my_object GET    /my_objects/:id(.:format)      my_objects#show
                  PATCH  /my_objects/:id(.:format)      my_objects#update
                  PUT    /my_objects/:id(.:format)      my_objects#update
                  DELETE /my_objects/:id(.:format)      my_objects#destroy

As you can see you have two same paths:

GET    /my_objects/:code(.:format)

and

GET    /my_objects/:id(.:format) 

This might cause a problem. If not a problem it is kind of duplicate and bad small code.

Upvotes: 1

Related Questions