【发布时间】:2015-01-26 15:43:12
【问题描述】:
我在Rails Tutorial 7.4.4,我很好奇以下测试帖子表单中的post_via_redirect 方法以及authenticity_token 参数如何。
以下测试将通过:
class UsersSignupTest < ActionDispatch::IntegrationTest
test "valid signup information will add user to database" do
assert_difference 'User.count', 1 do
post_via_redirect users_path, user: { name: "Filius Flitwick",
email: "Filius_Flitwick@Hogworts.ORG",
password: "charmsmaster",
password_confirmation: "charmsmaster" }
end
end
end
为了防止 CSRF (Cross Site Request Forgery),我假设表单中没有正确的authenticity_token 参数是不会通过验证的。但是,我无法弄清楚authenticity_token 是从哪里放入parameters 的。
事实上,我不确定rails 中的POST 到底在做什么。 POST会先请求一个网页的URL来获取authenticity_token吗?
【问题讨论】:
标签: ruby-on-rails forms csrf restful-url