I am passing hard time with ruby on rails ...I was trying to count the
average of a whole table......at first I calculated avarage of eah column
then average of the average values but it is giving me an error- here are
my codes and error-
error-

NoMethodError in Rate_schools#index

Showing */home/vmuser/workspace/project3
-3/app/views/rate_schools/index.html.erb* where line *#36* raised:

undefined method `+' for nil:NilClass

Extracted source (around line *#36*):

33:
34:
35:
36: Average <%=((@avg1+@avg2+@avg3+@avg4+@avg5+@avg6+@avg7+@avg8)/8)%>
37: <% end %>
38:
39: </tr>



codes In controller:
def index

@school=School.find(params[:id])
@rate_school =@school.rate_schools.build


@avg1=@school.rate_schools.average(:library)
@avg2=@school.rate_schools.average(:careercenter)
@avg3=@school.rate_schools.average(:location)
@avg4=@school.rate_schools.average(:food)
@avg5=@school.rate_schools.average(:healthcenter)
@avg6=@school.rate_schools.average(:studentactivities)
@avg7=@school.rate_schools.average(:facilities)
@avg8=@school.rate_schools.average(:internetspeed)

codes in index :
<% if !@school.rate_schools.blank? %>

<td> library=<%=@avg1%> </td>
<td>careercenter=<%=@avg2%> </td>
<td>location=<%=@avg3%> </td>
<td>Food=<%=@avg4%> </td>
<td>Healthcenter=<%=@avg5%> </td>
<td>studentactivities=<%=@avg6%> </td>
<td>facilities=<%=@avg7%> </td>
<td>internetspeed=<%=@avg8%> </td>





Average <%=((@avg1+@avg2+@avg3+@avg4+@avg5+@avg6+@avg7+@avg8)/8)%>
<% end %>

</tr>
</table>



--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/wUYDoovBR1UJ.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Search Discussions

  • Fernando Almeida at May 20, 2012 at 3:10 pm
    You should have a nil value to any @avg, try to use

    @avgN = @school.rate_schools.average(:X) || 0



    2012/5/20 Roy <poddojaroy@gmail.com>
    I am passing hard time with ruby on rails ...I was trying to count the
    average of a whole table......at first I calculated avarage of eah column
    then average of the average values but it is giving me an error- here are
    my codes and error-
    error-

    NoMethodError in Rate_schools#index

    Showing */home/vmuser/workspace/project3
    -3/app/views/rate_schools/index.html.erb* where line *#36* raised:

    undefined method `+' for nil:NilClass

    Extracted source (around line *#36*):

    33:
    34:
    35:
    36: Average <%=((@avg1+@avg2+@avg3+@avg4+@avg5+@avg6+@avg7+@avg8)/8)%>
    37: <% end %>
    38:
    39: </tr>



    codes In controller:
    def index

    @school=School.find(params[:id])
    @rate_school =@school.rate_schools.build


    @avg1=@school.rate_schools.average(:library)
    @avg2=@school.rate_schools.average(:careercenter)
    @avg3=@school.rate_schools.average(:location)
    @avg4=@school.rate_schools.average(:food)
    @avg5=@school.rate_schools.average(:healthcenter)
    @avg6=@school.rate_schools.average(:studentactivities)
    @avg7=@school.rate_schools.average(:facilities)
    @avg8=@school.rate_schools.average(:internetspeed)

    codes in index :
    <% if !@school.rate_schools.blank? %>

    <td> library=<%=@avg1%> </td>
    <td>careercenter=<%=@avg2%> </td>
    <td>location=<%=@avg3%> </td>
    <td>Food=<%=@avg4%> </td>
    <td>Healthcenter=<%=@avg5%> </td>
    <td>studentactivities=<%=@avg6%> </td>
    <td>facilities=<%=@avg7%> </td>
    <td>internetspeed=<%=@avg8%> </td>





    Average <%=((@avg1+@avg2+@avg3+@avg4+@avg5+@avg6+@avg7+@avg8
    )/8)%>
    <% end %>

    </tr>
    </table>



    --
    You received this message because you are subscribed to the Google Groups
    "Ruby on Rails: Talk" group.
    To view this discussion on the web visit
    https://groups.google.com/d/msg/rubyonrails-talk/-/wUYDoovBR1UJ.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to
    rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at
    http://groups.google.com/group/rubyonrails-talk?hl=en.


    --
    Fernando Almeida
    www.fernandoalmeida.net

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Javier Quarite at May 20, 2012 at 3:14 pm

    On Sun, May 20, 2012 at 10:09 AM, Fernando Almeida wrote:

    You should have a nil value to any @avg, try to use

    @avgN = @school.rate_schools.average(:X) || 0

    I also suggest to pass all of that logic to the model so after finding the
    school:
    in controller
    @school = School.find(params[:id])
    @avg1 = @school.food_average

    in model
    def food_average
    rate_school.average(:food)
    end

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Padmoja Roy at May 20, 2012 at 3:23 pm
    Thanks a lot......it's working.....great group!
    On Sun, May 20, 2012 at 11:09 AM, Fernando Almeida wrote:

    You should have a nil value to any @avg, try to use

    @avgN = @school.rate_schools.average(:X) || 0




    2012/5/20 Roy <poddojaroy@gmail.com>
    I am passing hard time with ruby on rails ...I was trying to count the
    average of a whole table......at first I calculated avarage of eah column
    then average of the average values but it is giving me an error- here are
    my codes and error-
    error-

    NoMethodError in Rate_schools#index

    Showing */home/vmuser/workspace/project3
    -3/app/views/rate_schools/index.html.erb* where line *#36* raised:

    undefined method `+' for nil:NilClass

    Extracted source (around line *#36*):

    33:
    34:
    35:
    36: Average <%=((@avg1+@avg2+@avg3+@avg4+@avg5+@avg6+@avg7+@avg8)/8)%>
    37: <% end %>
    38:
    39: </tr>



    codes In controller:
    def index

    @school=School.find(params[:id])
    @rate_school =@school.rate_schools.build


    @avg1=@school.rate_schools.average(:library)
    @avg2=@school.rate_schools.average(:careercenter)
    @avg3=@school.rate_schools.average(:location)
    @avg4=@school.rate_schools.average(:food)
    @avg5=@school.rate_schools.average(:healthcenter)
    @avg6=@school.rate_schools.average(:studentactivities)
    @avg7=@school.rate_schools.average(:facilities)
    @avg8=@school.rate_schools.average(:internetspeed)

    codes in index :
    <% if !@school.rate_schools.blank? %>

    <td> library=<%=@avg1%> </td>
    <td>careercenter=<%=@avg2%> </td>
    <td>location=<%=@avg3%> </td>
    <td>Food=<%=@avg4%> </td>
    <td>Healthcenter=<%=@avg5%> </td>
    <td>studentactivities=<%=@avg6%> </td>
    <td>facilities=<%=@avg7%> </td>
    <td>internetspeed=<%=@avg8%> </td>





    Average <%=((@avg1+@avg2+@avg3+@avg4+@avg5+@avg6+@avg7+@avg8
    )/8)%>
    <% end %>

    </tr>
    </table>



    --
    You received this message because you are subscribed to the Google Groups
    "Ruby on Rails: Talk" group.
    To view this discussion on the web visit
    https://groups.google.com/d/msg/rubyonrails-talk/-/wUYDoovBR1UJ.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to
    rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at
    http://groups.google.com/group/rubyonrails-talk?hl=en.


    --
    Fernando Almeida
    www.fernandoalmeida.net


    --
    You received this message because you are subscribed to the Google Groups
    "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to
    rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at
    http://groups.google.com/group/rubyonrails-talk?hl=en.
    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedMay 20, '12 at 2:50p
activeMay 20, '12 at 3:23p
posts4
users3
websiterubyonrails.org
irc#RubyOnRails

People

Translate

site design / logo © 2021 Grokbase