curl linux 数组参数_如何用curl更新数组值

In How to post array values via curl it gives a great example of how to set array values via curl:

$ curl -X POST http://localhost:3000/api/v1/shops.json -d \

"shop[name]=Supermarket \

&shop[products][]=fruit \

&shop[products][]=eggs \

&auth_token=a1b2c3d4"

What I'm wondering is if there's a way to update an existing array via curl without overwriting the values?

A little more info on what I'm trying to do - I'm trying to create a rails survey app with a HABTM relationship between the users/surveys. After a user takes the survey (in my Android app) it should update the join table with the user/survey so the user isn't served the same survey again. I've been trying to replicate this via curl but have been running into some trouble.

I'm able to update a survey title via this PUT request in curl:

curl -v -H 'Content-Type: application/json' -H 'Accept: application/json' -X PUT -d '{"title":"123dddTestingPlease rddfsfsfrdf","id":3,"users":[{"id":2,"email":"test@testme.com","created_at":"2014-02-28T01:56:09.841Z","updated_at":"2014-02-28T01:56:09.879Z","admin":true,"auth_token":"7acac413cbc22049a3ebf074f45c9847"}]}' http://localhost:3000/surveys/1

But I'm having troubles updating the users array. Ideally it would just append the user I send to the existing user array.

Thanks in advance!

EDIT:

First to answer your question - the problem is the user array doesn't seem to be updating at all when I do the curl request. What my end goal is to do is when the user completes the survey on the Android app, I want to send a PUT message to the rails server that will update that survey to contain that user (when the app checks to see if a survey is available, it checks to see if the user has taken the survey).

Here's my survey controller (I stripped everything but Update for readability):

class SurveysController < ApplicationController

before_action :set_survey, only: [:show, :edit, :update, :destroy]

# PATCH/PUT /surveys/1

# PATCH/PUT /surveys/1.json

def update

respond_to do |format|

if @survey.update(survey_params)

format.html { redirect_to @survey, notice: 'Survey was successfully updated.' }

format.json { head :no_content }

else

format.html { render action: 'edit' }

format.json { render json: @survey.errors, status: :unprocessable_entity }

end

end

end

private

# Use callbacks to share common setup or constraints between actions.

def set_survey

@survey = Survey.find(params[:id])

end

# Never trust parameters from the scary internet, only allow the white list through.

def survey_params

params.require(:survey).permit(:title, :users)

end

end

I also tried a variant where I just send the user's auth_token and then I ID the user by that and try to add them to the users list, but that doesn't seem to work either. Here's what that code looked like:

def update

@survey = Survey.find(params[:id])

@survey.add_user params[:auth_token]

redirect_to api_v1_survey_path(@survey)

end

And the model:

class Survey < ActiveRecord::Base

has_many :surveytizations

has_many :questions, :through => :surveytizations

has_many :users, :through => :completions

has_many :completions

def complete_survey (survey, user)

survey.users << user

survey.number_taken += 1

if survey.number_taken == survey.survey_limit

survey.survey_finished = true

end

end

def add_user auth_token

user = User.find_user_by_token auth_token

completions.create(user_id: user.id)

end

def find_user(auth_token)

user = User.find_user_by_token auth_token

if (user != nil && completions.find_by(user_id: user.id) != nil)

return true

else

return false

end

end

def self.check_survey (auth_token)

Survey.all.each do |survey|

next if survey.survey_finished

next if survey.find_user auth_token

return survey.id

end

end

end

解决方案

I ended up figuring it out. here's what I changed my controller to:

def update

@survey = Survey.find(params[:id])

@user = User.find_user_by_token params[:auth_token]

@survey.users << @user

redirect_to api_v1_survey_path(@survey)

end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值