我有这样的哈希:
Some_hash =
{"Albania"=>"Europe",
"Andorra"=>"Europe",
"Austria"=>"Europe",
Lebanon"=>"Asia",
"Macau"=>"Asia",
"Malaysia"=>"Asia",
"Papua New Guinea"=>"Asia",
"Jamaica"=>"North America",
"Martinique"=>"North America",
"Argentina"=>"South America",
"Chile"=>"South America",
"Sao Tome and Principe"=>"Africa",
"Senegal"=>"Africa",
"Somalia"=>"Africa",}
Run Code Online (Sandbox Code Playgroud)
我想单独确定五大洲以及属于它们的国家,这样我最终会得到这样的结论:
{"Africa" => ["Senegal", "Somalia"]}
{"Europe" => ["Albania", "Andorra", "Austria"]}
Run Code Online (Sandbox Code Playgroud)
适用于所有大陆.
我试过这个:
def country
inflation_hash = {}
XPath.match( data, "//country").map do |element|
inflation_hash[element.attributes["name"]] = element.attributes["continent"]
end
inflation_hash.each do |country, continent|
new_hash = {}
if inflation_hash.has_value?("Africa") == true
new_hash["Africa"] = inflation_hash.keys
puts new_hash
end
end
end
Run Code Online (Sandbox Code Playgroud)
而且效果太好了.我得到一个新的哈希:
{Africa => []}
Run Code Online (Sandbox Code Playgroud)
但我有两个问题:
我认为第一个问题与each …
UsersProfileController具有强大的参数,如下所示:
def user_profile_params
params.permit(:age, :relations)
# yes, I am not requiring user_profile. Just permitting attributes I need.
end
Run Code Online (Sandbox Code Playgroud)
create动作通过父(has-one和belongs-to association)构建UserProfile
def create
parent = Parent.create_guest
parent.build_user_profile(user_profile_params)
if parent.save
# do something
else
# handle error
end
end
Run Code Online (Sandbox Code Playgroud)
在UserProfiles中调用params返回:
<ActionController::Parameters
{"age"=>"23",
"relations"=>"3",
"subdomain"=>"api",
"format"=>:json,
"controller"=>"api/v1/user_profiles",
"action"=>"create"}
permitted: false>
Run Code Online (Sandbox Code Playgroud)
调用user_profile_params,返回:
user_profile_params:
Unpermitted parameters: subdomain, format
<ActionController::Parameters
{"age"=>"23",
"relations"=>"3", }
permitted: true>
Run Code Online (Sandbox Code Playgroud)
当发布请求时,我希望能够使用user_profile_params中的白名单参数创建user_profile.相反,create
UserProfiles中的操作失败,错误:Unpermitted parameters: subdomain, format
.
这不是我的预期.我希望user_profile_params只包含允许的值并忽略所有其他值.
我可以添加:format
和:subdomain
列出允许的属性,但有些东西感觉有点不对劲.
有人可以解释发生了什么/我错过了什么?
ruby parameters ruby-on-rails strong-parameters ruby-on-rails-5
有些东西告诉我,我在测试中缺少一个关键的概念/想法,或者(天堂禁止)ruby如何初始化对象.
我有一个类方法接受两个参数并返回所述类的实例.所以它看起来像这样:
class Manager
def self.run(first_arg, second_arg)
new(first_arg, second_arg)
end
end
Run Code Online (Sandbox Code Playgroud)
这是我的RSpec测试:
RSpec.describe Manager, type: :api do
let(:first_arg) { FactoryGirl.build_stubbed(:first_arg) }
let(:second_arg) { AccountMailer.new }
describe '::run' do
it "accepts two arguments" do
expect(Manager).to receive(:run).with(first_arg, second_arg)
Manager.run(first_arg, second_arg)
end
it "instantiates the class with 2 arguments" do
expect(Manager).to receive(:new).with(first_arg, second_arg)
Manager.run(first_arg, second_arg)
end
end
end
Run Code Online (Sandbox Code Playgroud)
由于(我相信)该方法:initialize
被new调用,我将代码更新为:
class Manager
# add attr_reader for read access
attr_reader :first_arg, :second_arg
def initialize(first_arg, second_arg)
@first_arg = first_arg
@second_arg = second_arg
end
def …
Run Code Online (Sandbox Code Playgroud)