There are no route mappings in the controller specification for my complex route

I have this route:

resources :items, path: 'feed', only: [:index], defaults: { variant: :feed }

which is nested in the api and v1 namespaces. (request_source parameters come from the Api namespace).

I want to check the effect of the index in the specification of my controller. I tried:

get :feed, community_id: community.id, :request_source=>"api"

does not work, and so:

get :index, community_id: community.id, :request_source=>"api", variant: 'feed'

saying:

ActionController::RoutingError:
   No route matches {:community_id=>"14", :request_source=>"api", :variant=>"feed", :controller=>"api/v1/items"}

------- EDIT ---------

The reason I want to use the option of sending parameters to the controller is because I have all these routes:

    resources :items, path: 'feed',    only: [:index], defaults: { variant: 'feed' }
    resources :items, path: 'popular', only: [:index], defaults: { variant: 'popular' }

Then in the ItemsController, I have a filter before get_items for the index action, which does:

def get_items
  if params[:variant] == 'feed'
     ....
elsif params[:variant] == 'popular'
    ....
end
+5
source share
2 answers

The problem seems to be related to the definition defaults: { variant: :feed }. Could you talk about what you are trying to do with this.

, , config/routes.rb

namespace :api do
  namespace :v1 do
    resources :items, path: 'feed', only: :index
  end
end

, rake routes.

$ rake routes
api_v1_items GET /api/v1/feed(.:format) api/v1/items#index

:, params , , .

params[:variant] ||= 'feed'

2: params[:variant] .

class ItemsController < ApplicationController
  before_filter :get_variant

  # or more simply
  # before_filter lambda { params[:variant] ||= 'feed' }

  def index
    render text: params[:variant]
  end

private      

  def get_variant
    # make sure we have a default variant set
    params[:variant] ||= 'feed'
  end
end
+2

, , , . variant . .

namespace :api do
  namespace :v1 do
    root :to => 'items#index', :defaults => { :variant => 'feed' }
    get ':variant' => 'items#index', :defaults => { :variant => 'feed' }
  end
end

GET "http://localhost:3000/api/v1" # params[:variant] == 'feed'
GET "http://localhost:3000/api/v1/feed" # params[:variant] == 'feed'
GET "http://localhost:3000/api/v1/popular" # params[:variant] == 'popular'
0

All Articles