Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/53.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails post方法在RubyonRails中是如何工作的_Ruby On Rails_Ruby_Post - Fatal编程技术网

Ruby on rails post方法在RubyonRails中是如何工作的

Ruby on rails post方法在RubyonRails中是如何工作的,ruby-on-rails,ruby,post,Ruby On Rails,Ruby,Post,我来自Python和Java背景,对CSS、HTML、Ruby只有基本的了解,并试图学习使用RubyonRails进行web开发。我正试着按照上的教程进行学习。我不明白清单7.23中的post方法在做什么参数 require 'test_helper' class UsersSignupTest < ActionDispatch::IntegrationTest test "invalid signup information" do get signup_path

我来自Python和Java背景,对CSS、HTML、Ruby只有基本的了解,并试图学习使用RubyonRails进行web开发。我正试着按照上的教程进行学习。我不明白清单7.23中的
post
方法在做什么参数

require 'test_helper'

class UsersSignupTest < ActionDispatch::IntegrationTest

  test "invalid signup information" do
    get signup_path
    assert_no_difference 'User.count' do
      post users_path, params: { user: { name:  "",
                                         email: "user@invalid",
                                         password:              "foo",
                                         password_confirmation: "bar" } }
    end
    assert_template 'users/new'
  end
end
需要“测试助手”
类UsersSignupTest

根据我在中的跟踪,它接受了两个非可选参数,这两个参数都是字符串,但在清单7.23中,第二个参数中突然出现了哈希语法
params:
,这让我感到困惑。有人能告诉我吗?

我想你找错地方了,链接显示的是http.post
。您需要
integrationtestpost

发件人:

以及:

编辑:双splat

Ruby 2.0添加了关键字参数和双splat。 当参数数目未知时,基本上使用单个splat(*),并将其作为
数组
传递

def with_args(*args)
  p args
end

with_args(1,2,"a")
# [1, 2, "a"]
双splat(**)的作用类似于*,但对于关键字参数:

def with_args(**args)
  with_keyword(args)
end

def with_keyword(some_key: nil, other_key: nil)
  p "some_key: #{some_key}, other_key: #{other_key}"
end

with_args(some_key: "some_value", other_key: "other_value")
# "some_key: some_value, other_key: other_value"
with_args(some_key: "some_value")
# "some_key: some_value, other_key: "
在ruby中,您可以在不使用
()
的情况下调用方法,并在不使用
{}
的情况下传递散列,因此

with_args({some_key: "some_value", other_key: "other_value"})
就像写作一样

with_args some_key: "some_value", other_key: "other_value")
看看这个答案: 及

所以

写作时

post users_path, params: { user: { name:  "",
                                   email: "user@invalid",
                                   password:              "foo",
                                   password_confirmation: "bar" } }
正在处理的调用

process(:post, users_path, params: { user: { name:  "",
                                   email: "user@invalid",
                                   password:              "foo",
                                   password_confirmation: "bar" } }
过程
中,
参数
是散列

{ user: { name:  "",
  email: "user@invalid",
  password:              "foo",
  password_confirmation: "bar" } }
进程的其他关键字参数无关紧要,哈希值是all
params
,所有其他关键字都是nil


希望它有意义…

啊!好问题。这一行:

class UsersSignupTest

表示该类继承自
ActionDispatch::IntegrationTest

ActionDispatch::IntegrationTest
是一个Rails类。您正在查看
Net::HTTP
类的文档,这是一个Ruby类


从一开始,Ruby和Rails之间的混淆是很常见的。Rails是框架,Ruby是语言。

您好,谢谢您的回复,实际上我对我要研究的API(Rails、Ruby、HTTP、HTML、CSS)的数量感到非常困惑。通常我只是被教程难住了,我不知道他们是在重写现有的类方法,还是不知从何处实现了一个新的类方法。哦,这比我的答案可读得多,我只是查看了github:),实际上我也不了解API,正如你之前的答案所示,从我的基本理解来看,我认为
post(path,**args)
中的
**args
确实意味着需要传入一个散列,并且相同的散列正在传入
进程
,但是当
process
中有这么多关键字参数时,问题就出现了,我看不出散列中包含的键值对是如何映射到参数的。嗨,你介意告诉我
process
的方法调用是如何工作的吗?我已经在上面的答案中解释了我的一些思考过程,但我不能从教程的上下文中完全理解它。@PrashinJeevaganth当然,我能做到的。我用double-splat(**)语法编辑了答案,以及调用如何传递给进程。如果我错了,请纠正我。通过断言,我们使用了一个没有错误消息的函数,
User.count
被计算两次,一次在开始,然后在执行代码块后第二次,这是具有一些哈希参数的
post
函数,如本答案中所述
post
可能会由于
过程而返回某些内容,但也可能会更新计数,因此我们希望控制台在实际存在差异时发出“红色”的声音。不太确定assert的实现在做什么,但这是我的最佳选择。@PrashinJeevaganth如果您基本上是正确的,
User.count
将被计算两次。但测试是检查数字是否保持不变,因为这是创建无效用户的
post
请求。如果
post
请求将更新计数,则这是一个错误,测试应该失败。如果您查看实现,您将看到
assert\u no\u difference
仅使用0调用
assert\u difference
。的实现是:
assert\u difference
。实际上,我对此感到困惑:
post:create,article:invalid_attributes
assert_no_difference
中,
:有些东西通常意味着我们事先定义了
散列,想要访问相应键的值,但是你在哪里找到
:create
,因为它是一种方法而不是某种键?对于
文章
,同样的混淆。我只是通过阅读名称将其解释为您提到的内容,但我无法真正理解代码实现。
process(:post, users_path, params: { user: { name:  "",
                                   email: "user@invalid",
                                   password:              "foo",
                                   password_confirmation: "bar" } }
{ user: { name:  "",
  email: "user@invalid",
  password:              "foo",
  password_confirmation: "bar" } }