In Symfony 4, i try to pass parameter from my .env
to a controller:
in .env:
###> First user creation variables ###
FIRST_USER_LOGIN=admintest
FIRST_USER_PASSWORD=azertyui
###< First user creation variables ###
in services.yaml:
parameters:
locale: 'en'
first.user:
login: '%env(FIRST_USER_LOGIN)%'
password: '%env(FIRST_USER_PASSWORD)%'
in my controller:
$utilisateur->setUsername($this->getParameter('first.user.login'))
->setPassword(
$encoder->encodePassword(
$utilisateur,
$this->getParameter('first.user.password')
)
)
->setRoles(['ROLE_ADMIN']);
I don't know what I'm doing wrong but I've got this error message:
The parameter "first.user.login" must be defined.
Solved: just changed the services.yaml:
parameters:
locale: 'en'
first.user.login: '%env(FIRST_USER_LOGIN)%'
first.user.password: '%env(FIRST_USER_PASSWORD)%'
Related
i've installed google/cloud/translate in my project, the version in the Gemfile.lock is:
google-cloud-translate (2.1.0)
With the below code:
require "google/cloud/translate"
project_id = "<Project ID>" # from my Google Cloud Platform
translate = Google::Cloud::Translate.new version: :v2, project_id: project_id
That is what the documentation says and also what this answer related suggest (please note that i'm using v2 instead of v3)
RuntimeError: Could not load the default credentials. Browse to
https://developers.google.com/accounts/docs/application-default-credentials for more information
This part returns true:
require "google/cloud/translate"
Update
I already followed all the steps in:
https://cloud.google.com/docs/authentication/production
Created a service account, a credential key and set the env variable (on Windows), then I tried testing the credential configuration with the google/cloud/storage example and it's worked fine, but when I tried with: google/cloud/translate gem with
translate = Google::Cloud::Translate.new version: :v2, project_id: project_id
I still got the same error
What can be the error?
Thanks in advance for any help.
Hi for doing this you will need to have your .json file with all the keys of your google service account, once that you have that file in the same path of your project you could do something like the following code:
module GoogleTranslator
require "google/cloud/translate"
ENV["GOOGLE_APPLICATION_CREDENTIALS"] = "yourfile.json"
class Translate
attr_reader :project_id, :location_id, :word, :target_language
def initialize(project_id, location_id, word, target_language)
#project_id = project_id
#location_id = location_id
#word = word
#target_language = target_language
end
def translate
client = Google::Cloud::Translate.translation_service
parent = client.location_path project: project_id, location: location_id
response = client.translate_text(parent: parent, contents: word.words.to_a, target_language_code: target_language)
translate_content(response)
end
def translate_content(response)
word.translation(response)
end
end
end
class Word
attr_reader :words
def initialize(words)
#words = words
end
def translation(words)
words.translations.each do |word|
puts word.translated_text
end
end
end
module GoogleTranslatorWrapper
def self.translate(project_id:, location_id:, word:, target_language:)
GoogleTranslator::Translate.new(project_id, location_id, word, target_language)
end
end
GoogleTranslatorWrapper.translate(project_id: "your_project_id_on_google", location_id: "us-central1", word: Word.new(["your example word"]), target_language: "es-mx").translate
Hope this can be clear :)...!
Create a service account:
gcloud iam service-accounts create rubyruby --description "rubyruby" --display-name "rubyruby"
Get the service account name:
gcloud iam service-accounts list
Create the credential key file for your service account:
gcloud iam service-accounts keys create key.json --iam-account rubyruby#your-project.iam.gserviceaccount.com
Set the env variable:
export GOOGLE_APPLICATION_CREDENTIALS=key.json
Enable the translate API
Install the library:
gem install google-cloud-translate
Edit the ruby.rb script
# project_id = "Your Google Cloud project ID"
# text = "The text you would like to detect the language of"
require "google/cloud/translate"
text = 'I am home'
translate = Google::Cloud::Translate.new version: :v2, project_id: project_id
detection = translate.detect text
puts "'#{text}' detected as language: #{detection.language}"
puts "Confidence: #{detection.confidence}"
Run the script:
ruby ruby.rb
Output:
'I am home' detected as language: en
Confidence: 1
I am trying to convert ruby hash object to YAML format using YAML.dump(obj) but I am getting improper indentation even after using dump options.
I have below executable ruby script :
#!/usr/bin/ruby
require "yaml"
require "erb"
context_path = ARGV[0]
context = YAML.load_file(context_path)['context']
def get_yaml(obj)
YAML.dump( obj['imports']['external_repositories']['credentials'] ).sub(/.*?\n/,'')
end
The value of - obj['imports']['external_repositories']['credentials'] is
{"iacbox"=>{"basic"=>{"name"=>"", "password"=>""}}, "nexus"=>{"basic"=>{"name"=>"cpreader", "password"=>"swordfish"}}}
Note : I used the sub method to remove "---" at the start of the output
The ERB template calls the above get_yaml method as :
credentials:
<%= get_yaml( context ) %>
The output that is coming is :
credentials:
iacbox:
basic:
name: ''
password: ''
nexus:
basic:
name: cpreader
password: swordfish
while I am expecting the output as :
credentials:
iacbox:
basic:
name: ''
password: ''
nexus:
basic:
name: cpreader
password: swordfish
How can I get the expected output from a dump?
I think the easiest thing for you to do here is just put the credentials key also in the Hash, i.e. change your template snippet so that it is one line:
<%= get_yaml( context ) %>
And change your get_yaml method to be:
def get_yaml(obj)
YAML.dump({'credentials' => obj['imports']['external_repositories']['credentials']})
.sub(/.*?\n/,'')
end
If that doesn't work for you, for example, if you have additional keys underneath the credentials key that you haven't mentioned, you could also do something like this:
def get_yaml(obj)
YAML.dump(obj['imports']['external_repositories']['credentials'])
.sub(/^---\n/,'')
.gsub(/\n/m,"\n ")
end
Where gsub(/\n/m,"\n ") replaces all newlines with a newline plus two spaces.
I am migrating my 4.2 app into Rails 5 and I run into a strange problem. When I try to log in via this:
<%= password_field_tag :password %>
I got [FILTERED] log in server console, but I got params[:pasword] => "*****" in controller. Which is of course not matching password (I am using BCrypt).
password encrypting:
def encrypt_password(pass)
BCrypt::Engine.hash_secret(pass, password_salt)
end
When I pass string "*****" into this method I got the same result as if I pass params[:password] there, which means the controller doesn't know about inserted password but gets only 5 asterisks.
If I try this on my master branch with Rails 4.2.7 then in controller I can correctly see inserted password (in log there is "[FILTERED]" correctly).
Do I miss some new feature about filtering parameters in Rails 5???
EDIT:
I tried
render plain: params.to_yaml
I got:
--- !ruby/object:ActionController::Parameters
parameters: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
utf8: "✓"
authenticity_token: 6/qK4IYcTL3lDTLNEICMmrcBhR5FJy6r3QNN5hYXNQTOqrdwtcX5aye4wN3AIz6PSmsMSf6V7/z8fmUo7KUXyQ==
login: doktor
password: "*****"
commit: Přihlaš
permitted: false
I have the config.yml which contains
dev:
url: http://dev.com
host: dev.com
port: 1234
stage:
url: http:// abc.stage.xyz.com
host: stage.com
port: 4567
prod:
url: http://nnn.prod.com
host: prod.com
port: 5698
In the feature file I write in scenarios as
scenario1: navigate to dev url
Given I navigate to the dev url
scenario2: navigate to stage_url
Given I navigate to the stage url
scenario3: navigate to prod_url
Given I navigate to the prod url
In the step definition
Then /^I navigate to the (.*) url$/ do |type|
env_config = $config.<type>.to_hash
#example
#env_config = $config.dev.to_hash
#env_config = $config.stage.to_hash
#browser.go_to(env_config['url'])
end
$config in evn.rb as
FigNewton.yml_directory = 'config'
FigNewton.load('config.yml')
$config = FigNewton
Question :- How I can map the type to the $config in the same format I mentioned in the example.It doesn't work like env_config = $config[type]
I have a the following data in tmp.yml. My goal is to load the data to mysql database.
I have the following code to load the data from tmp.yml:
$LOAD_PATH.unshift( File.join( File.dirname(__FILE__), 'lib' ) )
require 'yaml'
require 'AacflmDirection' # Just an empty class
SITE_PATH = '/var/www/test/testme/asian_cinema/tmp.yml'
doc = YAML::load( File.open( SITE_PATH ) )
puts doc[0]['attributes']['position'] # Expect position = 1
And I got this error. It seems I cannot access it via hash.
load.rb:8:in `<main>': undefined method `[]' for #<AacflmDirection:0x000000023c9fe0> (NoMethodError)
tmp.yml
--- !ruby/object:AacflmDirection
attributes:
position: "1"
film_id: "1"
created_on: 2012-02-06 09:31:31
page_id: "2671"
director_id: "1"
id: "1"
site_id: "173"
director:
film:
page:
site:
--- !ruby/object:AacflmDirector
assets:
attributes:
slug: paul-cox
name: Paul Cox
bio_markup: ""
created_on: 2012-02-06 09:31:39
page_id: "2671"
id: "51"
bio:
site_id: "173"
directions:
draft:
films:
page:
pathprints:
settings_objects:
site:
You're unserializing objects, not hashes. doc[0] is an instance of AacflmDirection. You need to access them with whatever accessors they provide.
Try doc[0].position.
First, three hyphens --- separate multiple documents in yaml. Then the !ruby/object... followed will deserialize your file to an object, not hash.
In your original code, you only get the AacflmDirection object as your doc variable. Use YAML::load_stream to load all the objects in your yaml.
require 'AacflmDirector'
require 'AacflmDirection'
doc = YAML::load_stream( File.open( SITE_PATH ) )
By that you'll get:
=> [#<AacflmDirection:0x007fa62c1c3a48
#attributes=
{"position"=>"1",
"film_id"=>"1",
"created_on"=>2012-02-06 17:31:31 +0800
"page_id"=>"2671",
"director_id"=>"1",
"id"=>"1",
"site_id"=>"173"},
#director=nil,
#film=nil,
#page=nil,
#site=nil>,
#<AacflmDirector:0x007fa62c1bbe10
#assets=nil,
#attributes=
{"slug"=>"paul-cox",
"name"=>"Paul Cox",
"bio_markup"=>"",
"created_on"=>2012-02-06 17:31:39 +0800,
"page_id"=>"2671",
"id"=>"51",
"bio"=>nil,
"site_id"=>"173"},
#directions=nil,
#draft=nil,
#films=nil,
#page=nil,
#pathprints=nil,
#settings_objects=nil,
#site=nil>]
Then add attr_accessor :attributes in your AacflmDirection class defination. So you can get the value by:
doc[0].attributes["position"]
Use YAML::load_file(SITE_PATH) to read the file and convert it to a Ruby object.