Search code examples
ruby-on-railsruby-on-rails-pluginsoutput-formatting

print the multiple outputs stored in a variable on HTML page in rails in formatted way


I am new to rails, I have used rails count to get a count of my working and non working assets and stored them in variables. The count is grouped by asset type. When I try to print the count on my index page, its printing the count but in raw format. I need to print it in proper format. Here is the code from my controller-

@instock_working_type=EquipmentAsset.count(:conditions=>"oos=0 and location='In Stock'", :group=>"asset_type")
@instock_non_working_type=EquipmentAsset.count(:conditions=>"oos=1 and location='In Stock'", :group=>"asset_type")

now I have to print @instock_working_type and @instock_non_working_type on my index page for which I have used

<td><b>working Devices Type wise</td>
            <td><%=h @instock_working_type %></td>


    <td><b>Non working Devices Type wise</td>
            <td><%=h @instock_non_working_type %></td>

But the out put which I am getting on the browser is something like -

 {"MVT600"=>1} {"MVT380"=>1, "MVT600"=>2}

which I want in proper format like- MVT380=1 MVT600=2

Please help me in this.


Solution

  • You are getting a hash in instance variable, loop through it and display the values accordingly after accessing.

    @hash_var.each do |key,val|
       "#{key}=#{val}"
    end
    

    your current @instock_working_type is hash which is displayed on browser, it's a hash which has been grouped using the criteria as key. replace @hash_var with @instock_working_type and try