我正在使用 AngularJS 资源来基本上 $.ajax() 一些表单数据。
Post.put({user:$scope.getLoggedInUser(), action:"new"},{body:$scope.postBody, "public":$scope.postPublic}, function(post) {
表单上有一个名为“public”的复选框。
我正在使用此函数将表单数据与 URL 中的参数合并:
(defn get-params [request]
(merge (:params request) (parse-string (slurp (request :body)) true)))
true
当我从我的路由处理程序中 println时,当复选框被选中时,我会得到一个很好的结果:
(println (:public (get-params request)))
但是,当我将参数(即不是 println)传递给控制器中与数据库通信的另一个函数时,我在该函数的开头执行另一个 println 并nil
改为获取。
我试过把它作为
(read-string x)
(boolean (Boolean/valueOf x))
(Boolean/valueOf x)
无济于事。
可能导致它的一件事(但我不知道为什么)是我通过如下身份验证包装请求:
(auth? request #(create x y z))
该(create)
函数在数据库中创建一条记录。
true
无论我尝试了什么,我都无法做到。
编辑:更完整的代码
控制器
(defn auth? [request callback-true & [callback-false]]
(println (callback-true))
(let [login-response (auth/login request)]
(if (and (not (string? login-response))
login-response)
(callback-true)
(if callback-false
(callback-false)
(json-response (str "{\"auth\":" login-response "}"), 401)))))
(defn create [user logged-in-user body public?]
(if (= logged-in-user user)
(json-response (post-view/single-post (post/create user body public?)))
(json-response "{\"auth\":\"can't create post under different logged in user\"}" 401)))
(defroutes routes
....
(PUT "/api/:user/new" request
(println request)
(auth? request
#(create (:user (request :params))
(:user (request :session))
(:body (get-params request))
(:public (get-params request)))))
....
)
模型
(defn create [username body public?]
(println public?)
(when-not (and
(str/blank? body)
(str/blank? username))
(let [user-id (:id (get-id-from-username username))
new-post-id
(:id
(sql/with-connection db
(sql/insert-values :post
[:usr_id :body :public] [user-id body (Boolean/valueOf public?)])))]
(by-id new-post-id))))