简体   繁体   中英

How to access request parameters in a view and iterate over hash in ruby on rails

I am trying to access the requests parameters submitted over the form in a view and iterate over the hash values.

My definition in a view:

def show
  @final =final_params
end

My Request parameters:

def final_params
  params.permit(:Name, :Address, :MobileNumber, :"City")
end

My view:

<%= @final.each { |key, value| puts "#{key} #{value}" } %>

In my Show view it gives an empty array: enter image description here

Using console I see the data in forms data section:

.utf8: ✓
authenticity_token: Lxkt/WUyAqFvlLXcP7YsQY3PjhQ9yJFosByJwAfYRyoZseC4gaZj4J8cC4EJs/LLGBIMypbggpajmtCH3um2dA==
final[Name]: Test
final[Address]: Hyd
final[MobileNumber]: 8456213254
final[City]: Del
commit: Submit

Get rid of puts in your view, it's not needed.

<% @final.keys.each do |key| %>
  key: <%= key %> <br/>
  value: <%= @final[key] %>
<% end %>

Or you can probably just do

<%= @final.inspect %>

If that's still empty then no params are being passed to your controller action. To sure add this in your controller and look at the logs:

def show
  @final = final_params
  puts "Final Params are set to: #{@final.inspect}" # or logger debug if puts doesn't output anything to your log
  logger.debug  "Final Params are set to: #{@final.inspect}"
  @final 
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM