jeudi 23 juin 2016

Rails subset of index route

Is it possible to make an (almost) duplicate of the RESTful controller#index route to show a subset of what might show on the index page?

Here's my scenario: I have a network_hosts controller and the index action is showing all network hosts. I'd like to make another route that shows a subset of those network_hosts, say, ones that have a certain validity score (as set by the user).

I thought I was on the right track, but what I've done so far tries to forward to the show action for the controller, so obviously I don't understand what I'm trying to accomplish.

routes.rb

resources :network_hosts
  get 'network_hosts/hosts_risk' => 'network_hosts#hosts_risk', as: :hosts_risk

network_hosts_controller.rb

class NetworkHostsController < ApplicationController

  def index
    @network_hosts = NetworkHost.all
  end

  def hosts_risk
    # only hosts below 99% validity
  end
end

views/network_hosts/ directory tree

 |views/
 |
 |__ network_hosts/
    |
    |__ index.html.erb
    |__ hosts_risk.html.erb

Running bundle exec rake routes shows the path exists:

new_network_host GET  /network_hosts/new(.:format)        network_hosts#new
edit_network_host GET /network_hosts/:id/edit(.:format)   network_hosts#edit
network_host GET      /network_hosts/:id(.:format)        network_hosts#show
             PATCH    /network_hosts/:id(.:format)        network_hosts#update
             PUT      /network_hosts/:id(.:format)        network_hosts#update
             DELETE   /network_hosts/:id(.:format)        network_hosts#destroy
hosts_risk   GET      /network_hosts/hosts_risk(.:format) network_hosts#hosts_risk

So, when I wire up the route with <%= link_to "Hosts at Risk", hosts_risk_path %> I get this error:

ActionView::MissingTemplate (Missing template network_hosts/show, application/show with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder, :haml]}. Searched in:
  * "/Users/godzilla74/Coding/neo-api/app/views"
  * "/Users/godzilla74/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/rails_admin_tag_list-0.2.0/app/views"
  * "/Users/godzilla74/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/rails_admin-0.8.1/app/views"
  * "/Users/godzilla74/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/kaminari-0.16.3/app/views"
  * "/Users/godzilla74/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/devise-3.5.2/app/views"
  * "/Users/godzilla74/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/apipie-rails-0.3.5/app/views"
):

What I have missed or done wrong?

Aucun commentaire:

Enregistrer un commentaire