简体   繁体   中英

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.

Instead of entering some data in the Service application, I want to do that in my app. So I have a form with several parameters that create a Member and save those parameters: :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.

I'm trying to do that by calling on a HTTParty function in my Member's Show view.

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. 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). 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.

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. Help! :)

members.rb model:

class Member < ActiveRecord::Base
end

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:

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:

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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