ruby - Rails - How do I change any key that has a string for a value in a hash into arrays -


i have following form

<%  if (current_user.mod_of_game?(@guide) unless current_user.nil?) %>    <%= form_for([@category, @page], url: update_pages_path) |f| %>    <%= render 'shared/error_messages', object: f.object %>       <%= select_tag 'content[key1]', options_for_select(['atk', 'def', 'hp', 'sing'], 'atk' ), { :multiple => true} %>        <%= select_tag 'content[key2]', options_for_select(['atk', 'def', 'hp', 'sing'], 'atk' ), { :multiple => true} %>       <%= select_tag 'content[key3]', options_for_select(['atk', 'def', 'hp', 'sing'], 'atk' ), { :multiple => true} %>       <%= select_tag 'content[key4]', options_for_select(['atk', 'def', 'hp', 'sing'], 'atk' ), { :multiple => true} %>       <%= f.submit "save"  %>    <% end %> 

when information submitted stored params hash so

content: {"key1"=>["atk", "def"], "key2"=>"hp", "key3"=>["hp", "sing"], "key4"=>"atk"} 

the hash saved db controller

 if @page.update (category_params)    @page.update(table_content: params[:content])    redirect_to show_pages_path(@category, @page)    flash[:success] = "updated."  else    render 'edit'  end 

i need values in hash arrays, if contains single string. can see key2 , key4 in content has values strings.

i'm not sure if can directly changing form setup i've has no success trying way. other alternative changing strings hashes in controller before saved db.

with either case how can done? (i guess changing hash values stings arrays in controller best considering user cant change there).

you can replace line:

@page.update(table_content: params[:content]) 

with:

content = params[:content].each_with_object({}) |(k, v), h|   h[k] = v.is_a?(string) ? [v] : v end @page.update(table_content: content) 

Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

c++ - Clear the memory after returning a vector in a function -

erlang - Saving a digraph to mnesia is hindered because of its side-effects -