I have a rails 3 helper function. This function simply takes an array of ojects and return images based on the url in each object. For some reason, I cannot seem to print the images...I just get back the array.
for example:
def my_helper(items)
items.each do |item|
image_tag(item)
end
end
this returns the array. I've tried assigning to a variable and outputting, but no luck. I've seen where people say just use item.join('<br/>') but I didn't get that to work.
Help appreicated
This should work:
def my_helper(items)
items.map do |item|
image_tag(item)
end.join('<br/>')
end
The each method returns the original list it iterates over (in your example, items). To get what you want, you can use map:
def my_helper(items)
items.map do |item|
image_tag(item)
end
end
Related
I'm using Postgresql and I'm not sure what I'm missing here, but calling my database in my
helper.rb:
def get_info
info = Scraper.find(1)
puts info
end
in my controller.rb via a post request, like such:
def create
helpers.get_info
end
is returning
#<Scraper:0x00007f96f9738ce0>
As opposed to the actual information from my database:
<Scraper id: 1, restaurant: => "subway">
Modify following code
def get_info
info = Scraper.find(1)
puts info
end
as
def get_info
Scraper.find(1)
end
This will work, no need to use puts info or even if you want to use puts then you can use
puts info.inspect
puts prints class and Object Id for complex object. To print details about object, you can use inspect:
puts info.inspect
For more details, you can checkout here: https://vitobotta.com/2011/01/17/prettier-user-friendly-printing-of-ruby-objects/
puts info.inspect
I suggest you to read the ruby on rails documentation
I have a view that shows all of my customers. I have a Customer model that is backing this view. There is a method in the model that will change an attribute from "" to a persons initials, "jh". Here is the method.
def print(item, attribute)
value = item.send(attribute)
if(value.blank?)
item.send(attribute + '=', "jh")
else
item.send(attribute + '=', "")
end
end
When I run this code from the console, it works perfectly. When I try it in the app, nothing changes. I am getting the proper 'value' that I expect, but it is never turned into an empty string. I am sure I am missing something simple here, but please help. Thanks.
I don't see any problem with this code, and I tried it on both a plain ruby object and on an ActiveRecord model and both worked as expected. So I suspect something funny is happening that is specific to your code.
I would suggest in any case that rather than construct a setter via string concatenation, you should use Ruby's native instance_variable_set:
def print(item, attribute)
value = item.send(attribute)
if(value.blank?)
item.instance_variable_set("##{attribute}", "jh")
else
item.instance_variable_set("##{attribute}", "")
end
end
One caveat with this method is that it will create an instance variable if none previously existed.
Does anyone know of a way to get collection_select to name its fields for the text methods' names and not their values?
I've got print_100, print_200, and print_500 and a plan to add more when necessary. I'd like the values of the select box to read from Billing all the fields that start with print_ so the select box would just have options like 100, 200, and 500.
f.collection_select(:print_quantity, Billing.all, :print_100, :print_100)
Any thoughts? Cheers.
I'm not as familiar with this part of rails as I'd like to be, so be gentle.
http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-collection_select
the syntax is
collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})
If you were to change the second parameter (method) to an actual method (rather than just the attribute that you want from the billing object) you can make the value whatever you would like.
If that doesn't work (or if you're not allowed to substitute the attribute for a method) then you may be able to make it work using the 5th or 6th parameters, value_method and text_method, which define what values should be applied to the tags.
Anyway, this answer is mostly to point you in (hopefully) the right direction, since I'm not certain of the method or how it works.
Good luck.
Thanks to #DavidDraughnn for the idea for this solution. I wrote a method in the relevant helper, thus:
def get_quantities
#quantities = {}
Billing.column_names.each do |a|
if a.match(/^print_/)
#quantities[a.delete "print_"] = a.delete "print_"
end
end
return #quantities
end
And I've adjusted collection_select to select, thus:
<% get_quantities %>
<%= f.select(:print_quantity, #quantities, {:prompt => "Please select..."}) %>
Hope that helps someone.
In Rails 3 how do I sort an array of strings with special characters.
I have:
[Água, Electricidade, Telefone, Internet, Televisão, Gás, Renda]
However when i invoke sort over the array Água gets sent to the end of the array.
here's my approach:
class String
def to_canonical
self.gsub(/[áàâãä]/,'a').gsub(/[ÁÀÂÃÄ]/,'A')
end
end
['Água', 'Electricidade', 'Telefone', 'Internet', 'Televisão', 'Gás', 'Renda'].sort {|x,y| x.to_canonical <=> y.to_canonical}
this proves to be usefull for other regexp aswell, the to_canonical method can be implemented they way that best suits you, in this example just covered those 2 regexp.
hope this alternative helps.
:)
The approach I used when I ran into the same issue (depends on iconv gem):
require 'iconv'
def sort_alphabetical(words)
# caching and api-wrapper
transliterations = {}
transliterate = lambda do |w|
transliterations[w] ||= Iconv.iconv('ascii//ignore//translit', 'utf-8', w).to_s
end
words.sort do |w1,w2|
transliterate.call(w1) <=> transliterate.call(w2)
end
end
sorted = sort_alphabetical(...)
An alternative would be to use the sort_alphabetical gem.
Hi
I wonder how to work around the problem I have with the pagination gem "Kaminari".
For what I've understood you cant paginate #user = User.all.page(5)?
But what if I have this code and want to paginate that, is it possible or do I need to change the code?
#price = Price.joins(:retailer, :retailer => :profile).
where(['product_id=? AND size_id=?', params[:prod_id], params[:si_id]]).
group(:retailer_id).order("SUM((prices.price * #{params[:amount].to_i}) + profiles.shippingCost)").all
The only thing I receive right now when applying.page(5) to that code is
undefined method `page' for #<Class:0x000001023c4558>
You don't need the .all because the joins call, along with where and group, is returning an array of objects for you that meet your criteria. Remove your .all and call page on the instance variable (which you might want to rename to #pages or something else plural).