0

I am developing an Rails 3.2 app. I need to get the values from the URL params and add them to an array so that I can use .to_sentence to print them out (for search).

These are my params (there are more on some search pages).

search=true&title=s&client=&tags=&status=&start_date=&end_date=

I tried this but that does not get only the values but both key and value.:

params.map(&:to_s)

Update

I figured out how to get the values now but I need not to map values that are nil.

<%= params.each_value.map{|value| value }.to_sentence %>

1 Answer 1

2

Try this:

values = params.values.reject(&:blank?) # ["true", "s"]

values returns all the values from the Hash, including the empty strings

.reject(&:blank?) is a shorthand for .reject { |v| v.blank? }. reject returns a new Hash, skipping all the values which blank? method returns true, so this eliminates all the blank values. Note that blank? also returns true for empty strings.

See the docs for these methods:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.