如何用curl更新数组值 [英] How to update array values with curl
问题描述
在如何通过以下方式发布数组值: curl 它给出了如何通过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"
我想知道的是,如果有一种方法通过curl更新现有的数组,而不覆盖这些值?
What I'm wondering is if there's a way to update an existing array via curl without overwriting the values?
一个更多的信息,我试图做 - 我试图创建一个轨道调查应用程序与用户/调查之间的HABTM关系。在用户进行调查(在我的Android应用程序)后,它应该更新用户/调查的连接表,以便用户不再被提供相同的调查。
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.
我可以通过curl的这个PUT请求来更新一个调查题名:
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
但我遇到麻烦更新users数组。
But I'm having troubles updating the users array. Ideally it would just append the user I send to the existing user array.
提前感谢!
这是我的调查控制器(我删除了一切,但更新为了可读性):
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
我也试过一个变种,我只是发送用户的auth_token,然后我ID的用户,并尝试添加到用户列表,但不是似乎也工作。代码如下:
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
和模型:
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
这篇关于如何用curl更新数组值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!