2016-11-07 1 views
1

ich die folgende Route haben:Warum ist current_page? einen 'no route error' zurückgeben, wenn ich es auf einer vollständig verwalteten Ressource mache?

resources :profiles do 
    member do 
     patch :speed_rating 
     patch :dribbling_rating 
     patch :passing_rating 
     patch :tackling_rating 
     post :favorite 
     post :unfavorite 
    end 
    collection do 
     get :autocomplete 
    end 
    end 

, dass die folgende Route erzeugt, die ich interessiert bin:

# truncated for brevity 
profiles_path GET /profiles(.:format) profiles#index 

profile_path GET /profiles/:id(.:format) profiles#show 

In meinem application.html.erb, habe ich dies:

<% if current_page?(controller: "profiles", action: "show")%> 
     <div id="page-wrapper" class="gray-bg"> 
    <% else %> 
     <div id="page-wrapper" class="image-bg"> 
    <% end %> 

Als ich gehe zu meinem Profiles#Show es funktioniert gut, aber wenn ich zu meinem Profiles#Index (aka mein root_path) gehe, bekomme ich etwas anderes:

Started GET "/" for ::1 at 2016-11-07 16:52:01 -0500 
    User Load (1.9ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 7], ["LIMIT", 1]] 
Processing by ProfilesController#index as HTML 
    Role Load (4.8ms) SELECT "roles".* FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHERE "users_roles"."user_id" = $1 AND (((roles.name = 'admin') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL))) [["user_id", 7]] 
    Role Load (12.9ms) SELECT "roles".* FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHERE "users_roles"."user_id" = $1 AND (((roles.name = 'coach') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL))) [["user_id", 7]] 
    Role Load (6.7ms) SELECT "roles".* FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHERE "users_roles"."user_id" = $1 AND (((roles.name = 'player') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL))) [["user_id", 7]] 
    (4.9ms) SELECT COUNT(*) FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHERE "users_roles"."user_id" = $1 AND (((roles.name = 'admin') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL)) OR ((roles.name = 'coach') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL))) [["user_id", 7]] 
    CACHE (0.0ms) SELECT "roles".* FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHERE "users_roles"."user_id" = $1 AND (((roles.name = 'player') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL))) [["user_id", 7]] 
    (2.6ms) SELECT COUNT(*) FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHERE "users_roles"."user_id" = $1 AND (((roles.name = 'coach') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL)) OR ((roles.name = 'admin') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL))) [["user_id", 7]] 
    Profile Search (23.5ms) curl http://localhost:9200/profiles_development/_search?pretty -d '{"query":{"match_all":{}},"size":1000,"from":0,"timeout":"11s","_source":false}' 
    Tournament Load (2.7ms) SELECT "tournaments".* FROM "tournaments" ORDER BY "tournaments"."id" ASC LIMIT $1 [["LIMIT", 1]] 
    Rendering profiles/index.html.erb within layouts/application 
    Profile Load (1.7ms) SELECT "profiles".* FROM "profiles" WHERE "profiles"."id" IN (14, 22, 12, 21, 9, 5, 4, 15, 6, 7, 16, 18, 13, 23, 17) 
Read fragment views//profiles/14-20161105042425134917/profiles/22-20161106175803133611/profiles/12-20161101225114614189/profiles/21-20161103035514173735/profiles/9-20161104221706306433/profiles/5-20161105043153971213/profiles/4-20161103035528589634/profiles/15-20161029013919242687/profiles/6-20161105043216951643/profiles/7-20161101001052922220/profiles/16-20161029020526832889/profiles/18-20161101223838805685/profiles/13-20161104221749051281/profiles/23-20161104062851335443/profiles/17-20161105043606243802/af785066fd895798884eea54748241db (0.1ms) 
    Rendered profiles/index.html.erb within layouts/application (12.5ms) 
Completed 500 Internal Server Error in 379ms (ActiveRecord: 36.4ms) 


ActionController::UrlGenerationError - No route matches {:action=>"show", :controller=>"profiles"}: 

Edit 1

Für die Aufzeichnung folgende Arbeiten ohne Probleme:

<% if (controller_name.eql? "profiles") && (action_name.eql? "show") %> 
+0

: controller => "profile" Sollte es nicht "Profile" sein? –

+0

@EricDuminil ist es. Mein Fehler. Ich habe getestet, ob es wichtig war oder nicht, wenn ich 'current_page? (Controller:" profile ")' 'verwende und die falsche Protokollausgabe eingefügt habe. Jetzt behoben. Auch für die Aufzeichnung war es egal. Keine funktionierte. – marcamillion

Antwort

0

Wenn Sie einen Blick auf current_page?source code nehmen Sie es url_for verwendet realisieren werde, die Braucht tatsächlich gültige Optionen, um eine URL zu erstellen und mit der Adresse übereinzustimmen, die Sie in der Adressleiste sehen.

url_for(controller: "profiles", action: "show") ist nicht in der Lage, diese Route ohne :id zu finden. Wenn Sie versuchen, current_page?(controller: "profiles", action: "show", id: 1) zu verwenden, werden Sie sehen, dass der Fehler nicht ausgelöst wird.

current_page ist nicht die beste Wahl für diesen Job, weil es entworfen ist für ein perfektes Spiel zu testen, einschließlich anderen Abfrage-String-Parametern wie:

# http://www.example.com/shop/checkout?order=desc&page=1 
current_page?(controller: 'shop', action: 'checkout', order: 'desc', page: '1') 

In Ihrem speziellen Fall, würden Sie es gerne entsprechen Jede Seite, die die Show-Aktion rendert. Ich denke, die Lösung, die Sie in Ihrem Edit 1 vorgestellt haben, ist geeignet. Sie können es immer in Ihrem eigenen Helfer einpacken, wenn Sie es aufräumen möchten.

Verwandte Themen