Ruby-on-rails Rails 解析参数时出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22504167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:27:31  来源:igfitidea点击:

Rails An Error Occured While Parsing Params

ruby-on-railsjsonparams

提问by Tom Hammond

I'm working on a basic survey app, where the user can take the survey in my Android app and then post the choices back to my rails server. At one point it was posting the choices fine to the server but it's recently starting throwing an error while parsing params and no longer creating the user's choices.

我正在开发一个基本的调查应用程序,用户可以在我的 Android 应用程序中进行调查,然后将选择发布回我的 rails 服务器。在某一时刻,它向服务器发布了很好的选择,但它最近开始在解析参数时抛出错误并且不再创建用户的选择。

I'm relatively new to HTTP and have been struggling with troubleshooting this error. Any help is much appreciated!

我对 HTTP 比较陌生,一直在努力解决这个错误。任何帮助深表感谢!

If I do this curl request:

如果我执行此卷曲请求:

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

I end up with this error in the stacktrace:

我最终在堆栈跟踪中出现此错误:

Started POST "/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'

Since it was working before I'm wondering if it has to do with something in my controller? The only change I made was changing User to Appuser though to reflect the actual data model of appusers being the folks taking the surveys:

因为它在我想知道它是否与我的控制器中的某些东西有关之前就可以工作了?我所做的唯一更改是将 User 更改为 Appuser 以反映 appuser 作为参与调查的人的实际数据模型:

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

Any ideas? I verified that the JSON is valid at jsonlint.com (but it is the same request that was being sent before).

有任何想法吗?我在 jsonlint.com 验证了 JSON 有效(但它与之前发送的请求相同)。

采纳答案by tirdadc

Since you have:

因为你有:

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

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

choiceis required and you should be passing your JSON like this:

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

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

回答by Egalitarian

The parameters need to be passed within quotes('')

参数需要在引号('')内传递

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

or

或者

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