【问题标题】:Grab json sent to rails抓取 json 发送到 rails
【发布时间】:2015-06-10 18:17:49
【问题描述】:

我有一个命令可以将一些 json 发布到 rails url:

curl -v -H "Accept: application/json" -H "Content-type: application/json" -X POST -d ' {"user":{"first_name":"firstname","last_name":"lastname","email":"email@email.com","password":"app123","password_confirmation":"app123"}}'  http://localhost:3000/maps

我的控制器中有一个方法创建:

def create
    #@map = Map.new(map_params)
    #@json = ActiveSupport::JSON.decode(open('jsonfeed').read)
    @result = JSON.parse string #parse json, but where to get that json first
    respond_to do |format|
      if @map.save
        format.json {
          #render :show, status: :created, location: @map
        }
      else
        format.json { render json: @map.errors, status: :unprocessable_entity }
      end
    end
  end

如何将 json 发送到 rails。我认为 JSON.parse 将解析我的 json 并返回哈希参数,但首先如何获取该 json。

【问题讨论】:

    标签: ruby-on-rails json ruby-on-rails-4


    【解决方案1】:

    在您的控制器中添加以下代码并尝试。

    before_action :parse_json_data_to_params, except: [ # Put Your Get Actions Names Here otherwise it will raise error for get requests ]
    
    def parse_json_data_to_params
      params.merge!(ActiveSupport::JSON.decode(request.raw_post))
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-06
      • 1970-01-01
      • 1970-01-01
      • 2016-09-19
      • 1970-01-01
      • 1970-01-01
      • 2018-12-13
      • 2016-01-09
      相关资源
      最近更新 更多