2016-02-20 64 views
0

我可以通過API在我的網站上刪除和更新主題,但由於某種原因,我無法弄清楚爲什麼我無法創建主題。無法通過API創建主題

IM我的終端

curl -H "Content-type: application/json" -H "Authorization: Token jX4h98/B3Jx9R0JXYtqeRjzNBzLJs/AFbXckBnkcGKKcuSMUFJ5dqpPbDgFki4G0D5TVrbfvR685Jjo6CQ2qUg==" -X POST -d '{"topic": {"name":"Drastic Voyage: Part II", "description":"The team\'s mission inside Dr. Kovacs goes awry, and they are blacklisted by the CIA."}}' http://localhost:3000/api/v1/topics/ 

上運行,並且我得到的是我的終端上>

這裏是我的創建方法在我的API主題控制器:

def create 
    topic = Topic.new(topic_params) 

    if topic.valid? 
     topic.save! 
     render json: topic.to_json, status: 201 
    else 
     render json: {error: "Topic is invalid", status: 400}, status: 400 
    end 
    end 

我的路線:

namespace :api do 
    namespace :v1 do 
     resources :users, only: [:index, :show, :create, :update] 
     resources :topics, except: [:edit, :new] 

    end 
    end 

任何想法可能我是做錯了什麼?

回答

1

您不能在shell中的單引號內使用單引號。如果你絕對需要在一行中完成,你必須將字符串分成兩半,並使用連接,如:The team'\''s mission inside。完整的命令如下。

curl -H "Content-type: application/json" -H "Authorization: Token jX4h98/B3Jx9R0JXYtqeRjzNBzLJs/AFbXckBnkcGKKcuSMUFJ5dqpPbDgFki4G0D5TVrbfvR685Jjo6CQ2qUg==" -X POST -d '{"topic": {"name":"Drastic Voyage: Part II", "description":"The team'\''s mission inside Dr. Kovacs goes awry, and they are blacklisted by the CIA."}}' http://localhost:3000/api/v1/topics/ 

或使用郵遞員。這非常方便。 https://www.getpostman.com/

+0

謝謝你工作:) –