I got this question in a previous interview and couldnt do it , any idea?
What does this return? Where would it be used?
module ApplicationHelper
def show_flash
flash.map{|key, value| content_tag(:div, value, {:class => key})}
end
end
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The ‘flash’ is a ruby-on-rails convention for storing information generated in one request (say, “invalid username” or “session not found” or “thanks for buying from us” or “cart updated”) temporarily for being rendered into the next view from the client.
The flash is a hash-like object.
The
.mapmethod on hash-like objects will iterate over all items in the hash; in this case, the.mapmethod is being passed a block that accepts two parameters (which it nameskeyandvalue, because thekeycould be used to look up thevaluefrom the hash). The block uses thecontent_taghelper to output new<div>elements with the value from the hash and the CSS selector-classkey.So for a flash like this:
{:name => "sars", :food => "pizza"}It would emit HTML roughly like this:
<div class="name">sars</div><div class="food">pizza</div>.This is a clever little helper method that probably saves a fair bit of typing, but it makes some assumptions: order in the view doesn’t matter, all the keys are either in the CSS already or the CSS is prepared to handle unknown class elements in a graceful way. This helper might only be used once in a template, but it’d still be helpful to have as a method that could be dropped into other projects later.