Kris MP
Kris MP

Reputation: 2415

Query string param is always nil

This is how I retrieve the params[:status] through the routes:

namespace :admin do
  get 'provider/status/:status', as: 'status_provider', to: 'providers#index'`
end

But I always get params[:status] return nil:

if params[:status].nil?
  providers = Provider.order("#{sort_column} #{sort_direction}")
else
  providers = Provider.filter_by_params(params[:status]).order("#{sort_column} #{sort_direction}")
end

Here is the view:

<%= admin_status_provider_path("enabled") %>

UPDATED

I have also tried editing my view like this:

<%= admin_status_provider_path(status: "enabled") %>

<a href="<%= admin_status_provider_path(status: 'enabled') %>" class="btn btn-default">
  Enabled
</a>

But still same result.

Upvotes: 0

Views: 120

Answers (1)

Deepak Mahakale
Deepak Mahakale

Reputation: 23661

Your status is expecting :status to be integer so following changes should do the trick

namespace :admin do
  get 'provider/status/:status', as: 'status_provider', to: 'providers#index', constraints: { status: /.*/ }
end

this does the trick

Started GET "provider/status/abc" for 127.0.0.1 
Processing by ProvidersController#index as HTML
Parameters: {"status"=>"abc"}

Upvotes: 2

Related Questions