Can I concatenate the result of two helper methods in single erb output - ruby

I created a helper to output some text using erb
<%= helper_method %>
but if i tried to put it 2 times it will not work ..for eg..
<% =
helper_method
helper_method
%>
I am expecting the text twice...but I get only once...

Each <%= %> outputs a single string, so either join them in a single string, or do it twice.
<%= helper_method %> (or <%= ... -%> )
<%= helper_method %>
There are a variety of ways to concatenate; %Q, normal string interpolation, etc.

try something like
<%= %Q(#{helper_method} #{helper_method}) %>

<%= helper_method + helper_method %>

If you need to print a String multiple times(the string in your case comes from a helper), you can simply use the multiplier operator
#helper
def helper_method
"Text"
end
#view
<%= helper_method * 2 %>
The result in a new string like this: TextText
HTH

When you say <%= helper_method %>, = means print the output, but when you say
<% =
helper_method1
helper_method2
%>
Rails doesn't know which out put should print, because it has two methods:
helper_method1
helper_method2

Related

How to add a class to a number_field bound to model object

No idea why this isn't working, have read through a lot of doc examples, most of those focus on text_field, maybe number_field is weird? I know that number_field doesn't accept a length option...
<%= form_for #person do |p| %>
<%= p.number_field :zipcode, class: "short-number-field" %>
...
<% end %>
The html never renders with the class, how do I do this?
Note, this is not a number_field_tag, so the number of arguments is 2.
Thanks!

Use a variable name in an erb include call

I'm using Sinatra and I'd like to include another erb file in one of my views but using a variable for the file name.
I was thinking something like this may work but I get an error saying no such file could be found.
<%= erb :'layout/nav/#{device_type}' %>
Currently I'm using the below switch statement to achieve the desired results but the above would be cleaner and less code.
<% case device_type
when 'mobile'%>
<%= erb :'layout/nav/mobile' %>
<% when 'tablet' %>
<%= erb :'layout/nav/tablet' %>
<% else %>
<%= erb :'layout/nav/desktop' %>
<% end %>
Thanks
Giles
I think that could work, you just need to use double quotes to interpolate strings.
Try
<%= erb :"layout/nav/#{device_type}" %>

erb idiom to handle undefined variable

I'm trying to write some puppet .erb, I'd like to handle this "environment" variable if it's:
undefined
a string with newlines
an array.
I've got as far as this:
<% Array(environment).join("\n").split(%r{\n}).each do |f| %>
one line: <%= f %>
<% end %>
But haven't gotten around the undefined case yet. I've tried this
<% if (defined?(environment)).nil? %?
<% Array(environment).join("\n").split(%r{\n}).each do |f| %>
one line: <%= f %>
<% end %>
<% end %>
but am still getting "(erb):11: undefined local variable or method `environment' for main:Object (NameError)" when trying to test it like this:
ruby -rerb -e "environmentUNDEFINEME= [ 'cronvar=cronval', 'var2=val2' ];
puts ERB.new(File.read('templates/job.erb')).result"
Sorry this is so basic, but somebody's got to ask the easy questions. Any help?
I would do this:
<% if defined?(environment) %>
<% Array(environment).each do |f| %>
one line: <%= f %>
<% end %>
<% end %>
I didn't understand why you joining on new lines and then splitting on them again, so I removed it from the example.

How to create a sequence of numbers in a Ruby on Rails form

I have 10 form fields and I want to append a number next to each field, going sequentially from 1 to 10. The problem is that the code is already in a loop. It's inside a partial, and the partial is passed a collection.
<%= fields_for "list", f do |f| %>
<!-- I want 1, 2, etc to appear here depending on the iteration. -->
<%= f.label :name %>
I tried using <%= i += 1 %> but it does not work since i is not defined. If I define i, it will keep getting reset to the same number, so it makes no difference. Any ideas?
Rails automatically defines a local variable called partialname_counter where, obviously, "partialname" is the name of your partial. So if your partial is called e.g. _list_item.html.erb you could write it like this:
<%= fields_for "list", f do |f| %>
<%= list_item_counter + 1 %>
<%= f.label :name %>
<% end %>
(The + 1, of course, is there because the counter starts at 0.)
Another option would be to just let the browser do the numbering for you using an ordered list:
In the view:
<ol>
<%= render :list_item, :collection => #some_items %>
</ol>
...and in the partial:
<%= fields_for "list", f do |f| %>
<li>
<%= f.label :name %>
</li>
<% end %>
This option is probably more semantic, and lists are easy to style in CSS.

hash enumeration and using outside arguments

I have a form builder and the normal |f| variable. In the middle I'd like to iterate over some hash and get a list of keys by which to build the form. How can I incorporate "f" as an argument to the enumerator.
<%= form_for ..... do |f| %>
<% available_types.each do |k,v| %>
<%= f.text_filed :selection, :value => v %>
<% end %>
<% end %>
As you can see in the code above, "f" is outside of the scope. Ideas?
f should still be in the scope. You misspelled text_field though.

Resources