简体   繁体   English

HTTParty不在POST请求中发送对象参数

[英]HTTParty not sending object parameters in POST request

I'm building a basic admin rails application for a Service I use to make creating new users more efficient. 我正在为我用来使创建新用户更有效的服务构建一个基本的管理导轨应用程序。 Right now, I'm doing it manually, within the application, and it has some tedious steps that I'd like to automate. 现在,我正在应用程序中手动执行此操作,并且它具有一些想要自动化的乏味步骤。

The service considers users as "Members" 该服务将用户视为“会员”

So I've created a Members scaffold in my rails project which has the same parameters as members do in the Service. 因此,我在rails项目中创建了一个Member支架,该支架的参数与Service中的成员相同。

Instead of entering some data in the Service application, I want to do that in my app. 我不想在Service应用程序中输入一些数据,而是希望在我的应用程序中进行输入。 So I have a form with several parameters that create a Member and save those parameters: :usname :usemail :usstudio_uid 所以我有一个带有几个参数的表单,这些表单创建一个Member并保存这些参数:usname :usemail :usstudio_uid

Next, I want to POST to the Service API with the initial fields that were entered to create an "invitation" for the new member. 接下来,我想使用输入的初始字段将服务发布到服务API,以为新成员创建“邀请”。

I'm trying to do that by calling on a HTTParty function in my Member's Show view. 我正在尝试通过在“会员的显示”视图中调用HTTParty函数来做到这一点。

My form is saving the parameters correctly, I'm connecting to the Service API via HTTParty and creating an invitation OK, but the Member parameters I want to send aren't populating. 我的表单正确保存了参数,我通过HTTParty连接到Service API并创建了一个邀请OK,但是我要发送的Member参数没有填充。 Instead it's turning what I thought was a reference to the parameter in plain text. 相反,它转向了我认为纯文本中对参数的引用。

Snippet of that function: 该功能的代码段:

:body => {"callback_url" => "https://foo.com/#invitations", "consumer_key" => "my consumer_key", "email" => :usemail,

I want :usemail to reference the parameter of the Member being referenced in the show page(ie cliff@foo.com). 我希望:usemail引用在显示页面中引用的成员的参数(例如,悬崖@ foo.com)。 Instead, as you'll see below, the Service API instead thinks the parameter is a string, returning "usemail" as the email parameter, not that of the Member object I want to reference. 相反,正如您将在下面看到的那样,Service API认为该参数是字符串,返回“ usemail”作为email参数,而不是我要引用的Member对象的参数。

I'm pretty new to rails and coding, so it's probably an obvious answer, but I spent a good 6 hours yesterday trying to figure it out. 我对Rails和编码还很陌生,所以这可能是一个很明显的答案,但是昨天我花了6个小时努力弄清楚。 Help! 救命! :) :)

members.rb model: Members.rb模型:

class Member < ActiveRecord::Base
end

members_controller rb: Members_controller rb:

  Class MembersController < ApplicationController
  before_action :set_member, only: [:show, :edit, :update, :destroy]

  respond_to :html

  def index
    @members = Member.all
    respond_with(@members)
  end

  def show
    @member = Member.find(params[:id])
  end

  def new
    @member = Member.new
    respond_with(@member)
  end

  def edit
    respond_with(@member)
    @member.save
  end

  def create
    @member = Member.new(member_params)
    @member.save    
    respond_with(@member)
  end

  def update
    @member.update(member_params)
    respond_with(@member)
  end

  def destroy
    @member.destroy
    respond_with(@member)
  end

  private
    def set_member
      @member = Member.find(params[:id])
    end

    def member_params
      params.require(:member).permit(:usname, :usemail, :usphone, :uspassword, :usconfirm_password, :usinvitation_uid, :usstudio_uid, :uscallback_url, :usmember_uid, :ususername, :usaccess_token) 
    end
end

In my members_helper.rb: 在我的members_helper.rb中:

def getinvitation(member)

  result = HTTParty.post "https://foo.com/api/v2/studios/studio_uid/invitations", :body => {"callback_url" => "https://foo.com/#invitations", 
          "consumer_key" => "my consumer_key", "email" => :usemail, 
          "name" => :usname, "studio_uid" => :usstudio_uid
           }.to_json, :headers => {'X-Auth-Token' => "my token"}
           JSON.parse(result.body)
          x = ActiveSupport::JSON.decode(result.body)

    end 

I call on the function in the member's show path: 我在成员的show路径中调用该函数:

views/members/show.html.erb

<%= getinvitation(@member) %>

Here is the response I get: 这是我得到的答复:

   {"uid"=>"29ad0740f4aa47d788bb2a34e9ab7d78", "studio_uid"=>"ORfspJitFbVG",
     "date_sent"=>"Sun Feb 1 16:49:21 2015", callback_url"=>"https://app.ustudio.com/#invitations?
    invitation_uid=29ad0740f4aa47d788bb2a34e9ab7d78&studio_uid=ORfspJitFbVG", 
    "consumer_key"=>"my consumer_key", "email"=>"usemail", "name"=>"usname"}

Basically, this is what I was looking for 基本上,这就是我想要的

"email" => member.usemail not :usemail or "usemail" "email" => member.usemail不是:usemail"usemail"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM