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 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

  • Colin Law at Dec 9, 2011 at 4:33 pm

    On 9 December 2011 14:22, YUJIN CHOI 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

    --
    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.
  • Frederick Cheung at Dec 9, 2011 at 5:17 pm

    On Dec 9, 2:22 pm, YUJIN CHOI 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

    --
    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
postedDec 9, '11 at 4:23p
activeDec 9, '11 at 5:17p
posts3
users3
websiterubyonrails.org
irc#RubyOnRails

People

Translate

site design / logo © 2022 Grokbase