2014-02-23 94 views
1

我正在嘗試製作一個web應用,幫助教師在線計算他們的成績。我使用lein new heroku命令創建它,並且無法讓我的路由器排序並顯示首頁。任何幫助,將不勝感激!這裏是我的defroutes的樣子。它是在同一個文件夾中的文件web.clj的頁面我想已經呈現:Clojure Heroku網頁應用:頁面不會在瀏覽器中呈現

(ns clojuregrade.web 
    (:require [compojure.core :refer [defroutes GET PUT POST DELETE ANY]] 
      [compojure.handler :refer [site]] 
      [compojure.route :as route] 
      [clojure.java.io :as io] 
      [ring.middleware.stacktrace :as trace] 
      [ring.middleware.session :as session] 
      [ring.middleware.session.cookie :as cookie] 
      [ring.adapter.jetty :as jetty] 
      [ring.middleware.basic-authentication :as basic] 
      [cemerick.drawbridge :as drawbridge] 
      [environ.core :refer [env]])) 

(defroutes app 
     (ANY "/repl" {:as req} 
      (drawbridge req)) 
     (GET "/" [] 
      {:status 200 ;; <- this is where it says the error is showing up 
      :headers {"Content-Type" "text/plain"} 
      :body (pr-str (slurp (io "landing.clj"))) ;; <- I am guessing here 
     (ANY "*" [] 
      (route/not-found (slurp (io/resource "404.html")))) 

這與頁面的代碼,我想已經呈現:

(ns clojuregrade.landing 
    (:require [compojure.core :refer [defroutes GET PUT POST DELETE ANY]] 
      [compojure.handler :refer [site]] 
      [compojure.route :as route] 
      [clojure.java.io :as io] 
      [ring.middleware.stacktrace :as trace] 
      [ring.middleware.session :as session] 
      [ring.middleware.session.cookie :as cookie] 
      [ring.adapter.jetty :as jetty] 
      [hiccup.core :refer :all] 
      )) 

      ... 

(defn home [& [weights grades error]] 
     (layout/common 
     [:h1 "Welcome to Clojure-grade"] 

     [:hr] 

     (form-to [:post "/"] 

       [:p "Enter the weights for your various grades below. For example if you have quizzes, homework and tests 
        and they are worth 40% 10% and 50% respectively, you can enter: [40 10 50]. 
        Of course, all of the numbers should add up to 100%. Be sure to include the brackets" 
        (text-field {:placeholder "[40 10 50]"} "weights" weights)] 

       [:p "Enter ALL of the grades for EACH STUDENT in your class. 
        Make sure that each of the grades is ordered such that the grade corresponds 
        to its matching weight above. Use brackets to separate students from each other. 
        Each set of grades should have the same number of grades as the number of possible weights (3 in this example case). 
        The following example shows grades for 4 students. Format your grades according to the number of students in your class:" 
        (text-area {:rows 40 :cols 40 :placeholder 
           "[89 78 63] 
           [78 91 60] 
           [54 85 91] 
           [100 89 77] 
           ..." } "grades" grades)] 
       (submit-button "process")))) 

    (defn process-grades [weights grades] 
    (->> (float grades) 
     (map (partial percentify-vector (float weights))) 
     (mapv #(apply + %)))) 

    (defroutes app 
     (GET "/" [] 
      {:status 200 
      :headers {"Content-Type" "text/html"} 
      :body home}) 
     (POST "/" [weights grades] ((process-grades (read-string weights) (read-string grades)))) 
     (ANY "*" [] 
      (route/not-found (slurp (io/resource "404.html"))))) 

    (defn wrap-error-page [handler] 
     (fn [req] 
     (try (handler req) 
      (catch Exception e 
       {:status 500 
       :headers {"Content-Type" "text/html"} 
       :body (slurp (io/resource "500.html"))})))) 

回答

2

有你的代碼有多個問題。首先,你有(io "landing.clj"),據我所知io是指一個命名空間,而不是一個有效的變種。但是,即使你有(io/resource "landing.clj")(和landing.clj是在資源目錄),這隻會轉儲landing.clj的內容(即源代碼)瀏覽器...可能不是你想要什麼。您需要撥打clojuregrade.landing/home。您可以直接從clojuregrade.web中的「/」路線執行此操作。你也可以(我在想什麼,你打算做),在clojuregrade.web派遣路由處理程序clojuregrade.landing(即調用clojuregrade.landing/app)中的「/」的路線。您可能還會發現https://github.com/weavejester/compojure/wiki/Nesting-routes有用。

順便說一句,你的「/」路線clojuregrade.landing看起來差不多正確。您可能需要實際調用home,然而,而不是一個參考返回功能:

(GET "/" [] 
     {:status 200 
     :headers {"Content-Type" "text/html"} 
     :body (home)}) ;; <-- Note parentheses -- (home) instead of home 

有可能仍然更需要做的就是一個運行的應用程序。如果你還沒有,請至少通過一個Ring/Compojure教程來獲得一個可用的應用程序,然後應用你學到的東西。

+0

非常感謝!我會實施你的建議並做更多的教程。我是全新的品牌:代碼,Clojure和網頁開發。學習曲線非常陡峭。我目前正在使用Clojure的書進行Web開發,並且已經從Github的頂部到底部閱讀了Ring和Compojure概述。我已經做了幾個教程,現在正在嘗試採取寶貝步驟來自己做一些事情。儘管如此,將它們結合在一起仍然是一項挑戰。任何固體(最新)教程建議,真正解釋了我需要的細節,將不勝感激。 – kurofune