我遇到了Rails4/mongoid應用程序中的API問題。我需要通過一個API操縱數據與python 3腳本,但我得到NoMethodError(未定義的方法「許可」爲「note_id」:字符串):
NoMethodError (undefined method `permit' for "note_id":String):
錯誤,當我嘗試提交請求。
我的Python代碼看起來像這樣
import requests
import json
url = 'http://0.0.0.0:3000/api/v1/note_proc_logs.json'
payload = {'note_proc_log' : { 'note_id' : '120904'}}
head = {"Authorization":"Token token=xxxxxxxxxxxxxxxxxxx"}
r = requests.post(url, payload, headers=head)
的API CONTROLER
module Api
module V1
# This class does not inherit from ApplicationController like the rest to skip Devise authentication
class NoteProcLogsController < ActionController::Base
before_filter :restrict_access if Rails.env.development? == false
respond_to :json
def create
Rails.logger.warn "note_proc_log_params: #{params}" if Rails.env.development?
@note_proc_log = NoteProcLog.new(note_proc_log_params)
respond_to do |format|
if @note_proc_log.save
format.json { render :show, status: :created, location: @note_proc_log }
else
format.json { render json: @note_proc_log.errors, status: :unprocessable_entity }
end
end
end
private
def restrict_access
authenticate_or_request_with_http_token do |token, options|
ApiKey.where(access_token: token).exists?
end
end
# Never trust parameters from the scary internet, only allow the white list through.
def note_proc_log_params
params.require(:note_proc_log).permit(:note_id)
end
end
end
end
只見幾個問題有相同的錯誤,但無法找到一個解決我的問題。
任何幫助將不勝感激。
UPDATE:
Rails.logger.warn "note_proc_log_params: #{params}" if Rails.env.development?
給我
W, [2016-07-25T15:10:38.362848 #48352] WARN -- : params: {"note_proc_log"=>"note_id", "format"=>"json", "controller"=>"api/v1/note_proc_logs", "action"=>"create"}
你在發展嗎?你能發佈這一行的結果嗎? Rails.logger.warn「note_proc_log_params:#{params}」如果Rails.env.development? 我想看看參數的格式。 – fabriciofreitag
我認爲你的有效載荷沒有爲參數生成適當的哈希 – hgsongra
從你的有效載荷密鑰和冒號':'在你的Python代碼中刪除空間,這裏是新的paylod payload = {'note_proc_log':{'note_id':'120904 '}' – hgsongra