i'm trying fetch value via controller , prints correctly in logs. however, when print in view gives error.
controller:
@leads = lead.all.order('updated_at desc') @leads.each |k| if k.lead_propertiesproperty_id = k.lead_properties.map(&:property_id) properties = property.where(:id => property_id) category_ids = properties.map(&:property_category_id) categories = propertycategory.where(:id => category_ids) @category_names = categories.map(&:name).exists? rails.logger.debug("categories: #{categories.inspect}") rails.logger.debug("category_names: #{@category_names.inspect}") else @properties = "no properties" @locations = "no locations" end end @leads_total = lead.all.count
view:
<tbody> <% @leads.each |lead| %> <tr class="<%=cycle('odd', 'even') %> location_row" id="lead_row" data-id="<%= lead.id%>"> <td><%= lead.id %></td> <td><%= lead.fullname %></td> <td><%= lead.email %></td> <td><%= lead.phone %></td> <td><%= @category_names.to_sentence %></td> <td><%= select_tag :status, options_for_select(lead.statuses.keys.to_a{|k, v| [k.humanize.capitalize, v]}, lead.status), :class => "lead_status"%></td> <td><%= lead.created_at.strftime("%d %b. %y - %t")%></td> <td><%= link_to (fa_icon "pencil-square-o "), edit_lead_path({:id => lead.id, :first_last_name => lead.first_last_name}), :title => 'edit lead', :class => "action-button" %></td> </tr> <% end %> </tbody>
the error: in controller log gives correct category (eg. desk, virtual, private) value each lead in view gives category value last lead, every lead.
if understand correctly it's because @category_names being assigned in loop.
so example if first time loop runs category "desk" @category_names becomes "desk"
however second time runs category becomes "virtual" overwrites @category_name variable.
you need push category_names hash id of lead
@category_names = {} @lead.each |lead| if lead.category_name.is_what_i_want? @category_names[lead.id.to_s] = lead.category_names end end
very pseudo should
then in view
@category_names[lead.id.to_s].to_sentence
Comments
Post a Comment