Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8040579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:56:40+00:00 2026-06-05T03:56:40+00:00

In application start up I wan to generate 10 persons with random data. When

  • 0

In application start up I wan to generate 10 persons with random data. When I do that I want to be able to filter them asynchronously by category. Here is the code:

Model code:

class Person   
   attr_accessor :name, :text, :category, :timeStamp
end

Controller code:

class PersonController < ApplicationController

  def index
    @persons = []
    @categories = ["history", "science", "religion"]
    10.times do
      person = Person.new
      person.name = [*('A'..'Z')].sample(8).join
      person.text = [*('A'..'Z')].sample(64).join
      person.category =  @categories.sample   
      person.timeStamp = Time.at(rand * Time.now.to_i)
      @persons.append(person)
    end
  end  

  def loadPersonsByCategory    
    @filteredPersons = @persons.where(:category => params[:category])
    render :json => @filteredPersons
  end
end

View code:

    <div id="personsTitle">Persons details</div>
    <div id="gridFilter">
        <label class="personText">Show persons from</label>
        <select id="filterCategory">
            <option selected="selected">All</option>
            <% @categories.each do |category| %>
                <option value="<%= category %>"><%= category %></option>
            <% end %>
        </select>
        <label class="personText">categories</label>        
    </div>
    <div id="personsGrid">
        <div class="gridRow gridHeader">        
            <div class="gridItem"><label class="personText">Name</label></div>
            <div class="gridItem"><label class="personText">Person's text</label></div>
            <div class="gridItem"><label class="personText">Category</label></div>
            <div class="gridItem"><label class="personText">Time stamp</label></div>
        </div>  
        <div id="gridBody"> 
            <% @persons.each do |person| %>
            <div class="gridRow">       
                <div class="gridItem"><label class="personText"><%= person.name %></label></div>
                <div class="gridItem"><label class="personText"><%= person.text %></label></div>
                <div class="gridItem"><label class="personText"><%= person.category %></label></div>
                <div class="gridItem"><label class="personText"><%= person.timeStamp %></label></div>
            </div>      
            <% end %>
        </div>  
        <div class="gridRow gridFooter">        
            <div class="gridItem"><label class="personText">First</label></div> 
            <div class="gridItem"><label class="personText"><</label></div> 
            <div class="gridItem"><label class="personText">1</label></div> 
            <div class="gridItem"><label class="personText">2</label></div> 
            <div class="gridItem"><label class="personText">3</label></div> 
            <div class="gridItem"><label class="personText">></label></div>     
            <div class="gridItem"><label class="personText">Last</label></div>      

JavaScript code:

$(function(){
    $('#filterCategory').on('change', FilterCategories);
});

function FilterCategories(){
    $.ajax(
        {
            type: "POST",
            url: "/person/loadPersonsByCategory",
            data: { category: $(this).val() },  
            contentType: "application/json; charset=utf-8",
            dataType: "json",             
            success: function (result) {
                var persons = [];               
                for (var i = result.length; i--; ) {
                    persons.push('<div class="gridRow">' +
                                        '<div class="gridItem"><label class="personText">' + result[i].name + '</label></div>' +
                                        '<div class="gridItem"><label class="personText">' + result[i].text + '</label></div>' +
                                        '<div class="gridItem"><label class="personText">' + result[i].category + '</label></div>' +
                                        '<div class="gridItem"><label class="personText">' + result[i].timeStamp + '</label></div>' +
                                '</div>');                                                        

                }

                $('<div/>', { 'class': 'filterResult', html: persons.join('')}).appendTo('#gridBody');
            },
             error:function (xhr, ajaxOptions, thrownError){
                    alert(xhr.status);
                    alert(thrownError);
            }    
        });
}

Everything is ok until I try to filter persons with ajax. I get 500 Internal sever error.
What can possibly cause this error? Thanks a lot!

Plus Server Error Log

Started POST "/Person/loadPersonsByCategory" for 127.0.0.1 at 2012-06-01 19:09:07 +0200
Processing by PersonController#loadPersonsByCategory as JSON
  Parameters: {"category"=>"religion"}
Completed 500 Internal Server Error in 1ms

NoMethodError (undefined method `where' for nil:NilClass):
  app/controllers/person_controller.rb:17:in `loadPersonsByCategory'

Routes.rb :

       TestAssignment::Application.routes.draw do
      get "person/index"

  match "/Person/loadPersonsByCategory", :controller => "person", :action => "loadPersonsByCategory"

      # The priority is based upon order of creation:
      # first created -> highest priority.

      # Sample of regular route:
      #   match 'products/:id' => 'catalog#view'
      # Keep in mind you can assign values other than :controller and :action

      # Sample of named route:
      #   match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase
      # This route can be invoked with purchase_url(:id => product.id)

      # Sample resource route (maps HTTP verbs to controller actions automatically):
      #   resources :products

      # Sample resource route with options:
      #   resources :products do
      #     member do
      #       get 'short'
      #       post 'toggle'
      #     end
      #
      #     collection do
      #       get 'sold'
      #     end
      #   end

      # Sample resource route with sub-resources:
      #   resources :products do
      #     resources :comments, :sales
      #     resource :seller
      #   end

      # Sample resource route with more complex sub-resources
      #   resources :products do
      #     resources :comments
      #     resources :sales do
      #       get 'recent', :on => :collection
      #     end
      #   end

      # Sample resource route within a namespace:
      #   namespace :admin do
      #     # Directs /admin/products/* to Admin::ProductsController
      #     # (app/controllers/admin/products_controller.rb)
      #     resources :products
      #   end

      # You can have the root of your site routed with "root"
      # just remember to delete public/index.html.
      root :to => 'person#index'

      # See how all your routes lay out with "rake routes"

      # This is a legacy wild controller route that's not recommended for RESTful applications.
      # Note: This route will make all actions in every controller accessible via GET requests.
      # match ':controller(/:action(/:id))(.:format)'
    end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T03:56:41+00:00Added an answer on June 5, 2026 at 3:56 am

    Remove this line

    contentType: "application/json; charset=utf-8",
    

    You post standard application/x-www-form-urlencoded data, but tell Rails to expect JSON encoded one. It tries to decode the data and fails on this step.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want that my application start after my setup is finished. I try to
How Many Times Does an ASP .NET Application Start? I want to run something
i wan to create a program that can get the application name i can
in my application start, i'm requesting data and parsing by communicating with web-service via
Heey, Does anybody know how to start safari from a ipad application? I wan't
I have a reporting MVC application that uses Castle Windsor. On application start up
I want to put some images in an area at the application start. As
I have app that has few schedule tasks that get initiated at application start.
On application start I'm creating Config object (Singleton). How could I make Config object
My idea is to make an application start automatically when a message from a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.