标签: params

将具有相同名称的params传递给Rspec POST请求以创建数组

我有一个API调用,我需要POST一组ID.我知道如果我通过调用ids[]我的请求的几个参数,它们将params在控制器的哈希中显示为数组:

# POST /api/events
# params:
# ids[] = 1 
# ids[] = 2
# ids[] = 3


# Then in Api::EventsController:

puts params # => { ids: [ "1", "2", "3"] }
Run Code Online (Sandbox Code Playgroud)

但是我怎么测试呢?在我的RSpec测试中,我不能两次使用相同的参数名称:

post "/api/events", :"ids[]" => 1, :"ids[]" => 2, :"ids[]" => 3
Run Code Online (Sandbox Code Playgroud)

因为这真正意味着:

post "/api/events", {:"ids[]" => 1, :"ids[]" => 2, :"ids[]" => 3}
Run Code Online (Sandbox Code Playgroud)

...和哈希不能有两次相同的键,所以第二个参数减少到只{:"ids[]" => 3}.

类似的东西:"ids[]" => "1,2,3"不起作用,它只是导致ids: ["1,2,3"].

我应该传递给post方法以在params控制器的哈希中获取数组?

ruby testing rspec ruby-on-rails params

1
推荐指数
1
解决办法
2284
查看次数

如何使params关键字使用类似于写入控制台

关于我的上一个问题(是否有一种更简单的方法将一组变量作为数组传递)

我试图将字符串变量传递给写入流的方法,而不是在每个单独的方法中进行写入.

使用params关键字显然是一种解决方案,但是通过使用它我相信我不能做这样的事情:

Write("hello {0}",var1);
Run Code Online (Sandbox Code Playgroud)

这没有使代码相当混乱.有没有办法强制这个功能到我自己的方法?

c# arguments params

0
推荐指数
1
解决办法
110
查看次数

从查询字符串中获取参数

在Rails中,我有一个关于如何获得多个参数的问题!例如:日志中的字符串就像这样

 Processing ConfigurationsController#emergency_config (for 192.168.1.124 at 2010-05-31 11:45:53) [POST]
  Parameters: {"authenticity_token"=>"I3GPKyrjmDRLkMIxFVS/47mgEI4ETO/+YW+R8R5Q2GM=", "tid"=>"1", "emergency"=>{"department"=>["1", "2", "3", "4", "5", "6", "7", "8"]}}
Run Code Online (Sandbox Code Playgroud)

那么,我怎样才能从中得到部门的价值呢?谁能告诉我答案?谢谢!

ruby ruby-on-rails params

0
推荐指数
1
解决办法
1861
查看次数

C#将数据添加到字符串数组中或从中删除数据

我希望通过提出这个问题,我找到一种更有效的方法来做我想做的事情.

首先,我有一个函数,其参数如下所示:

private void exampleVoid(string filePath, params string[] sourceFile) {

// Code ...

}
Run Code Online (Sandbox Code Playgroud)

**注意:我将'sourceFile'参数传递给CodeDom方法.如果任何参数为null,则此方法将引发错误.**

我的exampleVoid看法如下:

exampleVoid(@"C:\test.txt",
"Some Data",
"Some More Data",
"Even more data");
Run Code Online (Sandbox Code Playgroud)

在某些情况下,数组中的第二个字符串(标记为"更多数据")可能必须被删除而不会传递给该exampleVoid()方法.那么,是否有一些方法可以从阵列中删除此字符串?请记住,将字符串归零将不起作用,因为将抛出异常.

感谢您阅读,以及任何进一步的帮助.

埃文

c# arrays params

0
推荐指数
1
解决办法
388
查看次数

C#params明显的编译器错误(C#5.0)

这是我认为昨天解决的一个帖子的后续内容.昨天我在以下情况下遇到了我的代码问题:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
    class Program
    {
        class Bar
        {
            int v;

            public Bar(int v) { this.v = v; }
            public override string ToString() { return v.ToString(); }
        }

        static void Main(string[] args)
        {
            Foo(1, 2, 3);
            Foo(new int[] { 1, 2, 3 });
            Foo(new Bar(1), new Bar(2), new Bar(3));
            Foo(new Bar[] { new Bar(1), new Bar(2), new Bar(3) });
            System.Threading.Thread.Sleep(20000);
        }

        static void Foo(params object[] objs)
        {
            Console.WriteLine("New call …
Run Code Online (Sandbox Code Playgroud)

c# params c#-5.0

0
推荐指数
2
解决办法
786
查看次数

在两个php文件之间传递数组

我有一个需要重定向到另一个的PHP文件,但我需要将数组传递给第二个文件.我怎样才能做到这一点.

我知道这是错的,但我需要与此逻辑相似的东西.

<?php 
       $arr = array('this'=>'is', 'some'=>'stuff');
       header("someurl.php", vals=>$arr);
 ?>
Run Code Online (Sandbox Code Playgroud)

php redirect header params

0
推荐指数
3
解决办法
9306
查看次数

0
推荐指数
1
解决办法
43
查看次数

params [:id]来自rails?

我是Rails的初学者.我现在正在学习使用"Beginning Rails 4"这本书.我想问你关于传递给params方法的'参数'.以下是典型的轨道控制器之一.

class CommentsController < ApplicationController
  before_action :load_article
  def create
    @comment = @article.comments.new(comment_params)
    if @comment.save
      redirect_to @article, notice: 'Thanks for your comment'
    else
      redirect_to @article, alert: 'Unable to add comment'
    end
  end

  def destroy
    @comment = @article.comments.find(params[:id])
    @comment.destroy
    redirect_to @article, notice: 'Comment Deleted'
  end

  private
    def load_article
      @article = Article.find(params[:article_id])
    end

    def comment_params
      params.require(:comment).permit(:name, :email, :body)
    end
  end 
Run Code Online (Sandbox Code Playgroud)

是的,这只是一个典型的评论控制器,用于创建附加到文章的评论.评论模型"属于"文章模型,文章模型"有很多"评论.

看看destroy方法.

def destroy
  @comment = @article.comments.find(params[:id])
  -- snip --
end
Run Code Online (Sandbox Code Playgroud)

它通过find(params [:id])找到与文章相关的评论.我的问题是,params [:id]来自哪里?

它来自URL吗?或者,当创建任何评论记录时,rails会自动保存params散列吗?所以我们可以通过find找到任何评论(params [:id])?

load_article方法类似.

def load_article
  @article = …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails params

0
推荐指数
1
解决办法
3541
查看次数

嵌套属性错误:没有将字符串隐式转换为整数

我的项目模型属于我的订单模型,我的订单模型有很多项目。当我尝试传递以下参数时:

{"utf8"=>"?", "authenticity_token"=>"mPKksp+W5lZc7+lrc90trtCzX+UtPj4PI8boNf7vb+nneMF/J5SSBz8Nmh+CQ//DkmuVP2MJf7gS3oLqpZcM2Q==", "order"=>{"special_instructions"=>"", "item_attributes"=>{"topping_ids"=>["1", "2", "5"]}}, "commit"=>"Save"}
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

no implicit conversion of String into Integer
Run Code Online (Sandbox Code Playgroud)

该错误发生在我的Orders控制器中的create操作中:

@order = Order.new(order_params)
Run Code Online (Sandbox Code Playgroud)

这是我的params方法的样子:

def order_params
  params.require(:order).permit(:completed, :special_instructions, items_attributes: [ :size, topping_ids: [] ])
end
Run Code Online (Sandbox Code Playgroud)

这是我的订单模型:

class Order < ActiveRecord::Base
  belongs_to :user
  has_many :items
  accepts_nested_attributes_for :items
end
Run Code Online (Sandbox Code Playgroud)

这是我的物品模型:

class Item < ActiveRecord::Base
  belongs_to :order
  has_many :sizes
  has_many :item_toppings
  has_many :toppings, through: :item_toppings
  has_many :inverse_item_toppings, class_name: 'ItemTopping', foreign_key: 'item_id'
  has_many :inverse_toppings, through: :inverse_item_toppings, source: :item
  accepts_nested_attributes_for :sizes, reject_if: :all_blank, allow_destroy: true
end
Run Code Online (Sandbox Code Playgroud)

这是我架构的相关部分

create_table …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails params

0
推荐指数
1
解决办法
1302
查看次数

在 Python LXML XSLT 中设置(多个)参数的示例

寻找这个问题的解决方案有一段时间了,因为文档并不是很清楚。

我最终使用了下面的方法,并认为我会分享回来。

python xslt lxml params

0
推荐指数
1
解决办法
1980
查看次数

params参数和正常参数

using System;
using System.Collections.Generic;

namespace Generics
{
    class Minivan
    {
        public void foo(int z, int x)
        {
            Console.WriteLine("foo with two parameters");
        }
        public void foo(params int[] z)
        {
            Console.WriteLine("foo with two params parameter");
        }
    }
    class D
    {
        public static void Main()
        {
            Minivan car3 = new Minivan();
            car3.foo(10,20); // which method will be called here!!!
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

哪个foo方法被调用?为什么?

.net c# params method-resolution-order

-6
推荐指数
1
解决办法
87
查看次数