2014-03-19 58 views
7

我正在研究一個基本調查應用程序,用戶可以在我的Android應用程序中參與調查,然後將選擇發佈回我的Rails服務器。在某一時刻,它將選擇正確地發佈到服務器,但它最近在解析參數時不斷拋出錯誤,不再創建用戶的選擇。解析Params時發生錯誤

我對HTTP比較陌生,一直在努力解決這個錯誤。任何幫助深表感謝!

如果我這樣做捲曲要求:

curl -v -H 'Content-Type: application/json' -H 'Accept: application/json' -X POST -d {"question_id":"1","answer_id":"2"} http://ec2-54-186-132-102.us-west-2.compute.amazonaws.com:8080/questions/1/choices 

我結束了這個錯誤的堆棧跟蹤:

Started POST "https://stackoverflow.com/questions/1/choices" for 172.31.10.156 at 2014-03-19 11:15:02 +0000 
Error occurred while parsing request parameters. 
Contents: 



ActionDispatch::ParamsParser::ParseError (795: unexpected token at 'question_id:1'): 
    actionpack (4.0.0) lib/action_dispatch/middleware/params_parser.rb:53:in `rescue in parse_formatted_parameters' 
    actionpack (4.0.0) lib/action_dispatch/middleware/params_parser.rb:32:in `parse_formatted_parameters' 
    actionpack (4.0.0) lib/action_dispatch/middleware/params_parser.rb:23:in `call' 
    actionpack (4.0.0) lib/action_dispatch/middleware/flash.rb:241:in `call' 
    rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context' 
    rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call' 
    actionpack (4.0.0) lib/action_dispatch/middleware/cookies.rb:486:in `call' 
    activerecord (4.0.0) lib/active_record/query_cache.rb:36:in `call' 
    activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:626:in `call' 
    activerecord (4.0.0) lib/active_record/migration.rb:369:in `call' 
    actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call' 
    activesupport (4.0.0) lib/active_support/callbacks.rb:373:in `_run__516066872829663058__call__callbacks' 
    activesupport (4.0.0) lib/active_support/callbacks.rb:80:in `run_callbacks' 
    actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:27:in `call' 
    actionpack (4.0.0) lib/action_dispatch/middleware/reloader.rb:64:in `call' 
    actionpack (4.0.0) lib/action_dispatch/middleware/remote_ip.rb:76:in `call' 
    actionpack (4.0.0) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call' 
    actionpack (4.0.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call' 
    railties (4.0.0) lib/rails/rack/logger.rb:38:in `call_app' 
    railties (4.0.0) lib/rails/rack/logger.rb:21:in `block in call' 
    activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `block in tagged' 
    activesupport (4.0.0) lib/active_support/tagged_logging.rb:25:in `tagged' 
    activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `tagged' 
    railties (4.0.0) lib/rails/rack/logger.rb:21:in `call' 
    actionpack (4.0.0) lib/action_dispatch/middleware/request_id.rb:21:in `call' 
    rack (1.5.2) lib/rack/methodoverride.rb:21:in `call' 
    rack (1.5.2) lib/rack/runtime.rb:17:in `call' 
    activesupport (4.0.0) lib/active_support/cache/strategy/local_cache.rb:83:in `call' 
    rack (1.5.2) lib/rack/lock.rb:17:in `call' 
    actionpack (4.0.0) lib/action_dispatch/middleware/static.rb:64:in `call' 
    railties (4.0.0) lib/rails/engine.rb:511:in `call' 
    railties (4.0.0) lib/rails/application.rb:97:in `call' 
    rack (1.5.2) lib/rack/lock.rb:17:in `call' 
    rack (1.5.2) lib/rack/content_length.rb:14:in `call' 
    rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service' 

因爲這是工作之前,我想知道如果它做東西在我的控制器?我所做的唯一變化是改變用戶對APPUSER雖然反映appusers是鄉親採取調查的實際數據模型:

class ChoicesController < ApplicationController 
    before_action :set_choice, only: [:show, :edit, :update, :destroy] 
    skip_before_action :verify_authenticity_token 

    def index 
    @question = Question.find(params[:question_id]) 
    end 

    # POST /choices 
    # POST /choices.json 
    def create 
    @question = Question.find(params[:question_id]) 
    @choice = @question.choices.build(choice_params) 
    @choice.answer = Answer.find(params[:choice][:answer_id]) 
    @appuser = Appuser.find_user_by_token params[:choice][:user_auth_token] 
    @appuser.choices << @choice 
    @survey = Survey.find(params[:choice][:survey_id]) 
    @survey.appusers.push(appuser) 

    respond_to do |format| 
     if @choice.save 
     format.html { redirect_to question_choices_path, notice: 'Choice was successfully created.' } 
     format.json { redirect_to question_choices_path, status: :created, location: @choice } 
     else 
     format.html { render action: 'new' } 
     format.json { render json: @choice.errors, status: :unprocessable_entity } 
     end 
    end 
    end 


    # DELETE /choices/1 
    # DELETE /choices/1.json 
    def destroy 
    @choice.destroy 
    respond_to do |format| 
     format.html { redirect_to choices_url } 
     format.json { head :no_content } 
    end 
    end 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_choice 
     @choice = Choice.find(params[:id]) 
    end 

    def set_question 
     @question = Question.find(params[:choice][:question_id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def choice_params 
     params.require(:choice).permit(:appuser_id, :answer_id, :question_id) 
    end 
end 

任何想法?我驗證了JSON在jsonlint.com上是有效的(但它與之前發送的請求是相同的)。

+0

據我所知,參數應該是一個查詢類的字符串:'-D 「question_id = 1&answer_id = 2」' – yoavmatchulsky

+0

如果你單引號JSON? '{「question_id」:「1」,「answer_id」:「2」}' –

回答

8

中過去了,因爲你有:

params.require(:choice).permit(:appuser_id, :answer_id, :question_id)

choice是必需的,你應該像這樣傳遞你的JSON:

curl -v -H 'Content-Type: application/json' -H 'Accept: application/json' -X POST -d '{"choice": {"question_id":"1", "answer_id":"2"}}' http://ec2-54-186-132-102.us-west-2.compute.amazonaws.com:8080/questions/1/choices

+0

火箭應該是冒號 – Slicedpan

+0

是啊,固定。 – tirdadc

+0

它實際上是兩件事的結合,但現在它起作用了! –

6

的參數需要引號( '')

curl -v -H 'Content-Type: application/json' -H 'Accept: application/json' -X POST -d '{"question_id":"1","answer_id":"2"}' http://ec2-54-186-132-102.us-west-2.compute.amazonaws.com:8080/questions/1/choices 

curl -XPOST -H "Content-Type: application/json" http://ec2-54-186-132-102.us-west-2.compute.amazonaws.com:8080/questions/1/choices -d '{"question_id":"1","answer_id":"2"}' 
+0

這樣做,以及上述問題與選擇不在JSON –