2016-07-25 16 views
0

Rails4/mongoidアプリケーションでAPIに問題があります。私は、APIを介してのpython 3スクリプトを使用してデータを操作する必要があるが、私はリクエストを送信しようとすると、私はNoMethodError(未定義メソッド `permit_new_id ':String):

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?

問題はPythonスクリプトにあった私に

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"}

+0

開発中ですか?この行の結果を投稿できますか? Rails.logger.warn "note_proc_log_params:#{params}" if Rails.env.development? 私はparamsのフォーマットを見たいと思います。 – fabriciofreitag

+0

あなたのペイロードがパラメータ用の適切なハッシュを生成していないと思います – hgsongra

+0

ペイロードキーから空白を削除して、 'python code'でコロン': 'を、ここでは新しいpaylod' payload = {'note_proc_log':{'note_id': '120904 '} ' – hgsongra

答えて

0

を与えます。シンプルなpython辞書はペイロードとしてはOKですが、ネストされたものはそうではないようです。

私の最後のPythonスクリプトは、すべてがさえスペースを文字列に対して、親属性がそれぞれに指定する必要があり、文字列として追加の引用符を追加するので、必要性を全く扱われませんRailsの側では、この

import requests 
import json 

url = 'http://0.0.0.0:3000/api/v1/note_proc_logs.json' 
payload='note_proc_log[chip_id]=120904&note_proc_log[test_timestamp]=2016-07-19T13:24:49' 
head = {"Authorization":"Token token=xxxxxxxxxxxxxxxxxxxx"} 
r = requests.post(url=url, data=payload, headers=head) 

のように見えます子属性と要素は&で区切られます。

これは私にはうってつけのことです。それを行う方法が他にあるかどうかを知ることは興味深く、特に値の配列を含める方法があります。

関連する問題