What is the best way to return multiple tags from a Rails Helper?
There are several ways to do this.
Remember that the existing rails helpers like link_to
, etc, just output strings. You can concatenate the strings together and return that (which is what I do most of the time, if things are simple).
EG:
link_to( "something", something_path ) + #NOTE THE PLUS FOR STRING CONCAT
form.hidden_field('something'.tableize, :value=>'something')
If you're doing things which are more complicated, you could just put that code in a partial, and have your helper call render :partial
.
If you're doing more complicated stuff than even that, then you may want to look at errtheblog's block_to_partial helper, which is pretty cool
How to return multiple values from a Rails helper method
Concatenate the strings. The return value of the block will be the last line returned; you must concatenate the return values of the two link_to
calls.
def action_buttons(user)
case current_user.friendship_status(user)
# ...
when "requested"
link_to("Accept", ...) +
link_to("Decline", ...)
# ...
end
end
Rails Return multiple content_tags from Helper module
Use #concat:
http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#method-i-concat
this thread can be helpful:
rails, How to build table in helper using content_tag?
Rails 4: How to render a method with multiple returns
So, a helper method that is supposed to return something to render on a page, should return a string, not an array of multiple values.
You should decide yourself how you want the multiple values to display. How do you want them to display? Maybe just separated by a comma? Okay, so format it like that yourself inside the helper method:
...
return "#{savings}, #{num}"
end
updated answer for updated question
Update: Changed the return method:
return "<b>#{savings}€</b> en #{num}"
How i can render the html bold tag?
I'd do this:
def render_savings_for(savings, num)
safe_join(
content_tag("b", "#{savings}€"),
" en ",
num
)
end
Writing your own html-rendering helpers can look a bit confusing, but there are really just a few simple concepts around html safety to understand. I've previously tried to explain some of them here: https://bibwild.wordpress.com/2013/12/19/you-never-want-to-call-html_safe-in-a-rails-template/
Another alternative would be using a view partial instead of a helper method, which wouldn't require you to pay attention to html_safety tricks it would just take care of it for you, but for something this simple I think a helper method is simpler.
Next, you're going to start asking about i18n... make a new question! :)
Using multiple content_tag in one method in Rails
It's a weird quirk of content_tag but if you nest them you need to use a concat on each return in the inner tags. Otherwise, you just get the last returned string and the inner tags just disappear into the ether. Sadly, in my experience, I've found complex nesting isn't worth the effort of moving into a helper method.
Perhaps, a better approach would be to DRY up the html with a decorator pattern, rails partials, or using something like the cells gem.
each ... do returns an array when used in a helper method?
As Marek mentioned in comments each
do what it should do. You might want your method be like this:
def show_calendar
months = %w(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)
ul_body = months.map { |month| custom_tag(string) }
safe_join(ul_body, "<br />")
end
private
def custom_tag(string)
%(<li><span class="underlayer"></span><a href="#"> #{string} </a></li>)
end
Rails helper to return li for each category actually returning hash of categories
The each function is returning all the categories. So change the each method to map instead.
Unable to put two content_tags in a single helper method?
Add +
between 2 content_tag()
method:
def my_test
content_tag(:p, "One") + \
content_tag(:p, "Two")
end
The Ruby returns only a last expression from the method. To return all desired html you should add one to another.
Return Array in a helper method
You can.
Use find_all_by_parent_id
instead.
And you don't need the second line.
The following is enough.
def childrenOf(a)
@children = Post.find_all_by_parent_id(a.id)
end
Related Topics
How to Define_Method in Rails Models
Does Sublime Text Have Support for Ruby API (Auto-Complete)
How to Check If a Number Is Included in a Range (In One Statement)
Get Today's Date in Jekyll with Liquid Markup
App Pushed to Heroku Still Shows Standard Index Page
Namespacing Thor Commands in a Standalone Ruby Executable
How to Structure a Layout Template in Haml
How to Filter an Array of Hashes to Get Only the Keys in Another Array
Ubuntu 12 - How to Install Ruby and Rails Correctly
Ruby on Rails, Could Not Find a Valid Gem 'Rails'
Rails - How to Check Developer Mode or Production Mode in Code
What Is the Easiest Way I Can Create a 'Beep' Sound from a Ruby Program
Convert Datetime String to Utc in Rails
How to Load a Spec_Helper.Rb Automatically in Rspec 2
Are There Unintended Consequences of Ruby's 'Begin ... End' Without 'Rescue' Used as a Code Block
How to Check from Ruby Whether a Process with a Certain Pid Is Running
How to Switch to Older Versions of the Ruby/Rails Environment