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| %>
    <tr>
        <td><%= student.name %></td>
        <td><%= student.grade.grade %></td>
        <td><%= student.sex.sex %></td>
        <td><%= student.subject.subject %></td>
        <td><%= student.fee %></td>
        <td><%= student.address %></td>
    </tr>
<% end %>
---------------------------------------------------------------------------
---------------------------------------------------------------------------
---------------


  ++ search_controller.rb
---------------------------------------------------------------------------
---------------------------------------------------------------------------
---------------
class SearchController < ApplicationController
  def search
      @students = Student.where(params[:search]).all
  end
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 [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to