I have an action in the controller:
def user_detail
#user_detail = UserDetail.find_by_id(11)
end
And in the view:
<%= #user_detail -%> // displays me like #
I am trying to retrieve the contents of #user_detail: actually the hash contains {:empid=>"11111", :prjtname=>"aaaaa", :prjtrole=>"Developer"}
How do I display the user detail's empid and other values?
Since I know what question you asked earlier, I think this is the syntax you actually want to use:
<%= #user_detail.additional_info[:empid] %>
Unless of course you renamed the name of the hash :)
Another approach, if you want all the content from the hash but the keys varies from each record, you could loop through them like this:
<% #user_detail.additional_info.each_pair do |key, value| %>
<p>Key: <%= key %> Value: <%= value %></p>
<% end %>
To get simple debug output like the example you posted, this will handle it:
<%= #user_detail.inspect %>
try this <%= #user_detail.emplid %> <%= #user_detail.prjtname %> <%= #user_detail.prjtr %>
More of an extraction from #dln's answer
try using
<%= #user_detail[:emplid] %>
<%= #user_detail[:prjtname] %>
<%= #user_detail[:prjtr] %>
Hope this solves your prob
Related
I'm totally stuck on looping through a form.select in Rails (using Elastic Search/Searchkick aggs).
I can access the 'bucket' array (and thus "key" and "doc_count") when I don't pre-pend the form.select helper, but it just doesn't loop through when it's there. Not sure how I would get the options dynamically otherwise!
Does anyone know what might be going wrong (ignore the link_to stuff, I'll be changing that once I can get the actual "key")? Perhaps I'm using the form.select helper incorrectly? Thanks!
<% form.select #jobs.aggs["location"]["buckets"].each do |bucket| %>
<% if params[:location] == bucket["key"].to_s %>
<strong><%= link_to bucket["key"], request.params.except(:location)%></strong>
<% else %>
<%= link_to bucket["key"], request.params.merge(location: bucket["key"])%>
<% end %>
<% end %>
I figured it out! It was a syntax error on my part, as I forgot to use :location in the first part of the form.select, and additionally didn't wrap the block after this in [] brackets (presumably the options have to be in an array or hash with this helper).
Here is the code (simplified) which solved this for me:
<% form.select :location, [#jobs.aggs["location"]["buckets"].each do |bucket| %>
<%= bucket["key"] %>
<% end ] %>
In my controller I have:
#payment_types = [['Cash' ,'1'], ['Card', '2']]
What I'm trying to achieve is to show in view Cash and Card while writing on database 1 and 2.
In my view I tried:
<% payment_types.each do |payment_type| %>
<%= payment_type %>
<% end %>
which shows ['Cash' ,'1'] ['Card', '2']]
How can I show instead in my view Cash or Card?
I'm not sure if I understand your question, but if you want to show only 'Cash', and 'Card', you can do it by passing another argument (responsible for hash value, I called it _ because it's a convention for unused arguments) to your block, like this:
<% payment_types.each do |payment_type, _| %>
<%= payment_type %>
<% end %>
You could also do it like this
<% payment_types.each do |payment_type| %>
<%= payment_type.first %>
<% end %>
Is there a Ruby (preferably) or Rails way to check if the second index of an array exists?
In my Rails (4.2.6) app I have the following code in my view that shows the first two thumbnails for an array of photos:
<% if array.photos.any? %>
<%= image_tag array.photos.first.image.url(:thumb) %>
<%= image_tag array.photos[1].image.url(:thumb) %>
<% end %>
However if there is no second item in the array, then there is an error
I've tried the following if statements to make the rendering of the second thumbnail conditional, but they don't work:
<% if array.photos.include?(1) %>
<% if array.photos.second? %>
<% if array.photos[1]? %>
<% if array.photos[1].any? %>
I figured that another way to get what I want would be to simply check the length of the array
Still I was wondering if Ruby (or Rails) had a method or way to check if a specific index in an array exists or not. Thanks in advance
EDIT: To clarify I just want to show the first two thumbnails in the array, if any
You can use an .each, but if you want to follow this approach.
Instead of this:
<%= image_tag array.photos[1].image.url(:thumb) %>
Maybe you can use this:
<%= if(!array.photos[1].nil?) image_tag array.photos[1].image.url(:thumb) %>
Or:
<%= image_tag array.photos[1].image.url(:thumb) unless array.photos[1].nil? %>
Here, why not
(0...array.photos.size).each do |photo|
......
end
array.photos.each do |photo|
......
end
I am trying to create a checklist in rails using form_for. This checklist is taken from a table which I gained in the create action of my sign_ins controller:
#content = OrientationContent.where(site_id: session[:site_id])
In my view I want to use the form_for helper to iterate through the list in #content:
<%= form_for(:sign_ups ) do |f| %>
<% #content.each do |c| %>
<%= f.check_box nil %> <%= c %> <br>
<% end %>
<% end %>
However this is not working and it produces two square brackets on the page: [].
How do I go through the list and print the name while creating a check box on the left of it? The check box does not have any meaning or data, I just need it present for reference.
Solved:
In the controller, need to pluck an individual field:
#content = OrientationContent.where(site_id: 1).pluck(:content)
In the view, structure as so:
<%= form_for(:sign_ups) do |f| %>
<% #content.each do |c| %>
<%= f.check_box nil %> <%= c %> <br>
<% end %>
<% end %>
A really simple question but I just can't seem to get it working.
There is only one Property included below but there could be more than one within Properties. How can I iterate through this hash and display just the Name of each Property?
{"GetPropertiesResponse"=>{"Properties"=>{"Property"=>{"Breakfast"=>"IN", "Country"=>"GB", "Currency"=>"GBP", "Id"=>"1834", "Name"=>"Hotel Name"}}}}
I've tried this in my view:
<% #json['GetPropertiesResponse']['Properties']['Property'].each do |property| %>
<%= property['Name'] %>
<% end %>
I'm getting this error:
no implicit conversion of String into Integer
If you are saying there might be more than one property hash then this should work:
<% #json['GetPropertiesResponse']['Properties'].each do |property, value| %>
<%= value['Name'] %>
<% end %>
I would use #each_value on this hash since you don't appear to be using the keys
<% #json['GetPropertiesResponse']['Properties'].each_value do |value| %>
<%= value['Name'] %>
<% end %>
Should work. Note that the second line is <%= value['Name'] %> and not <%= property['Name'] %>
P.S. On a different note, I don't know why you're using the key "Property" inside of your Properties hash. That just seems like a good way to confuse yourself in exactly this way. Your keys within the Properties hash should be something unique to the property they describe. Since each will be a property, the string "Property" doesn't help describe or differentiate.