How do I get the value of a hash in rails?

2

I have some checkboxes inside a form in the view, that values are being sent to the controller by params for the index.

Obs. I am sending the values of the checkboxes of the view index to itself, I need the values of the checkboxes in a variable.

In controler I'm getting the params like this:

 @post = params[:checkbox_array]

In the view, you are:

post: <% @post %>

After the page refreshes, the variable with the second value appears:

post: {"2m"=>"1", "2"=>"2", "3m"=>"3"}

I want to just get all the keys or all the values and assign to another variable

As far as I know, it should be something like this in the controller:

@variavel1 = @post.key or @variavel2 = @post.values

@ post.select, @ post.sample ... also does not work, when I access the page I get "undefined method"

What should I do to filter this hash I'm getting and have an output like:

  •     
  • asked by anonymous 12.07.2017 / 05:27

    1 answer

    1

    You're right, the method you're looking for is keys or values , example:

    {"2m"=>"1", "2"=>"2", "3m"=>"3"}.keys
    => ["2m", "2", "3m"]
    
    {"2m"=>"1", "2"=>"2", "3m"=>"3"}.values
    => ["1", "2", "3"]
    

    I hope I have helped.

        
    12.07.2017 / 13:49