Rails3: search#result

Hi, I’m beginner of Rails in Korea.
I have a serious problem…

I wanna show the list of students filtered by options.

If I selected H1(high school 1), male, and English,
I wanna get the list of students who are H1, male, subject English.

But… I couldn’t…
“/search/result” screen just show ‘nil’ …

Please help me!!!

++ index.html.erb



    <%= form_tag("/search/result", :method => "get") do %>
        <%= select("search", "", @grades.map { |g| ["#{g.grade}",

g.id] }, { :include_blank => true }) %>
<%= select(“search”, “”, @sexes.map { |x| ["#{x.sex}",
x.id] }, { :include_blank => true }) %>
<%= select(“search”, “”, @subjects.map { |s|
["#{s.subject}", s.id] }, { :include_blank => true }) %>
<%= submit_tag(“Search”) %>
<% end %>



++ students_controller.rb



class StudentsController < ApplicationController

GET /students

GET /students.json

def index
@students = Student.all
@grades = Grade.find(:all)
@subjects = Subject.find(:all)
@sexes = Sex.find(:all)
respond_to do |format|
format.html # index.html.erb
format.json { render :json => @students }
end
end



++ student.rb (model)



class Student < ActiveRecord::Base
belongs_to :grade
belongs_to :subject
belongs_to :sex
validates :name, :presence => true
validates :grade_id, :presence => true
validates :sex_id, :presence => true
validates :subject_id, :presence => true
validates :fee, :presence => true
validates :address, :presence => true
def self.search(query)
where(“name like ?”, “%#{query}%”)
end
end



++ result.html.erb



<%= @students.inspect do |student| %>

<%= student.name %>
<%= student.grade.grade %>
<%= student.sex.sex %>
<%= student.subject.subject %>
<%= student.fee %>
<%= student.address %>

<% end %>



++ search_controller.rb



class SearchController < ApplicationController
def search
@students = Student.where(params[:search]).all
end
end

On 9 December 2011 14:22, YUJIN CHOI [email protected] wrote:

Hi, I’m beginner of Rails in Korea.
I have a serious problem…

I wanna show the list of students filtered by options.

If I selected H1(high school 1), male, and English,
I wanna get the list of students who are H1, male, subject English.

But… I couldn’t…
“/search/result” screen just show ‘nil’ …

There are several things you should do.

  1. Check the html of the page (View > Source or something similar in
    the browser) and check that the html is what you expect.
  2. Look in the log file (development.log assuming you are in
    development mode) and check that the parameters are being passed in
    the URL correctly.
  3. Have a look at the Rails Guide on debugging which will give you
    ideas on how to debug your code, in particular you can use ruby-debug
    to break into the code and inspect the data.

Colin

On Dec 9, 2:22pm, YUJIN CHOI [email protected] wrote:

<%= @students.inspect do |student| %>

that inspect won’t do any good. You probably meant each
I have a feeling that your form is a bit messed up too - if (like your
case) your form isn’t bound to an active model object then use
select_tag rather than trying to coerce the select helper into working
for you

Fred