Jelle
Jelle

Reputation: 45

How to get "key" from params?

<%= params[:select] %>  # key=qwerty secret=qwerty token=qwerty token_secret=qwerty

Please tell me how I can get "key" ? I do not understand:

<%= params{[:select[:key]]} %> # {"tweet"=>"", "select"=>"key=qwerty secret=qwerty token=qwerty token_secret=qwerty", "controller"=>"twitter_postings", "action"=>"index"}

Upvotes: 0

Views: 2461

Answers (2)

Mayur Shah
Mayur Shah

Reputation: 3449

Hope this will helping you.

params = {
  "tweet"=>"",
  "select"=>"key=qwerty secret=qwerty token=qwerty token_secret=qwerty",
  "controller"=>"twitter_postings",
  "action"=>"index"
}

for getting key value (qwerty) from this params following query will helping you.

params["select"].split.first.split("=").second
# => "qwerty"

steps: 1

params["select"].split
# => ["key=qwerty", "secret=qwerty", "token=qwerty", "token_secret=qwerty"]

find the value and split them

steps: 2

params["select"].split.first.split("=")
# => ["key", "qwerty"]

pick first value and split again with =

steps: 3

params["select"].split.first.split("=").second
# => "qwerty"

finally pick the second value.

Upvotes: 0

Sebasti&#225;n Palma
Sebasti&#225;n Palma

Reputation: 33420

You can access to the select key and over its value to split the content, getting the first one you have "key":

params = {
  "tweet"=>"",
  "select"=>"key=qwerty secret=qwerty token=qwerty token_secret=qwerty",
  "controller"=>"twitter_postings",
  "action"=>"index"
}
p params['select'].split.first
# "key=qwerty"

You can also turn it into a hash if it's easier for you:

select_hash = params['select'].split.each_with_object(Hash.new(0)) do |element, hash|
  key, value = element.split('=')
  hash[key] = value
end

p select_hash['key']
# "qwerty

Upvotes: 1

Related Questions