2009-08-28 5 views
1

私はRailsの初心者です。私は単純に小さなモデルを足場にしています。モデルにはcategoryというフィールドがあります。今、カテゴリ別にインデックスページのエントリをフィルタリングしたいと思います。Railsのフィルタフォーム?

<% form_for @domain do |f| %> 
<p> 
Domain: 
<%= f.select(:Domain,%w{ LifeStyle Automobiles FoodAndBeverage Health IT Telecom EntertainmentAndCelebrity Education BankingInvestmentAndInsurance Travel Sports Parenting ConsumerElectronics RealtyAndLogistics CauseLed})%> 


<%= submit_tag "Filter" %> 
</p> 
<% end %> 

<table border="1"> 
    <tr> 
    <th>Domain</th> 
    <th>Category</th> 
    <th>Course detail</th> 
    <th>Nameofblog</th> 
    <th>Descriptionofblog</th> 
    <th>Smename</th> 
    <th>Smecommuntiy</th> 
    <th>Smeifnotorkfac</th> 
    <th>Noofmemb</th> 
    <th>Discussionforumname</th> 
    <th>Discussionforumdescription</th> 
    <th>Qnasitesname</th> 
    <th>Qnasitesnamedesc</th> 
    <th>Newssitename</th> 
    <th>Newssitedesc</th> 
    </tr> 

<% @media_universe_entries.each do |media_universe_entry| %> 
    <tr> 
    <td><%=h media_universe_entry.Domain %></td> 
    <td><%=h media_universe_entry.Category %></td> 
    <td><%=h media_universe_entry.CourseDetail %></td> 
    <td><%=h media_universe_entry.NameOfBlog %></td> 
    <td><%=h media_universe_entry.Descriptionofblog %></td> 
    <td><%=h media_universe_entry.SMEname %></td> 
    <td><%=h media_universe_entry.SMECommuntiy %></td> 
    <td><%=h media_universe_entry.SMEIfnotOrkFac %></td> 
    <td><%=h media_universe_entry.NoOfMemb %></td> 
    <td><%=h media_universe_entry.discussionforumname %></td> 
    <td><%=h media_universe_entry.discussionforumdescription %></td> 
    <td><%=h media_universe_entry.QNASitesname %></td> 
    <td><%=h media_universe_entry.QNASitesnameDesc %></td> 
    <td><%=h media_universe_entry.NewsSiteName %></td> 
    <td><%=h media_universe_entry.NewsSiteDesc %></td> 
    <td><%= link_to 'Show', media_universe_entry %></td> 
    <td><%= link_to 'Edit', edit_media_universe_entry_path(media_universe_entry) %></td> 
    <td><%= link_to 'Destroy', media_universe_entry, :confirm => 'Are you sure?', :method => :delete %></td> 
    </tr> 
<% end %> 
</table> 

<br /> 

<%= link_to 'New media_universe_entry', new_media_universe_entry_path %> 

わかりましたように、私はドメインワイズ文字列を作成しようとしています。私はこれをどのようにして行うのですか?

答えて

5

コントローラのメソッドを投稿して、モデルをよりよく理解する必要があります。

とにかく、「何かが文字列「カテゴリ」列を持っていると仮定して、これらの値が固定されていない限り、選択値をハードコードしないでください。これは図である

# something_controller.rb 
    def index 
    @categories = Something.find_by_sql("SELECT category FROM somethings GROUP BY category").map &:category 
    @somethings = params[:category].blank? ? Something.all : Something.find_all_by_category(params[:category]) 
    end 

はここで速い型付けされた例です

<% form_tag(:action => :index) do %> 
    <%= select_tag "category", options_for_select(@categories) %> 
    <%= submit_tag "Filter" %> 
<% end %> 

<table> 
    <tr> 
    <th>foo</th> 
    ... 
    </tr> 
    <% somethings.each do |something| %> 
    <tr> 
     <td><%= something.foo %></td> 
     ... 
    </tr> 
    <% end %> 
</table> 

更新:上記のコードは動作しますが

、それは本当にあまりにもfastcodedました、悪い習慣を示唆している。ここで

は、より良い方法です:

# app/models/Something.rb 
def self.all_categories 
    find_by_sql("SELECT category FROM somethings GROUP BY category").map(&:category).select {|x| x} 
end 

def self.select(category) 
    if category 
    find_all_by_category(category) 
    else 
    find :all 
    end 
end 

#app/controllers/something_controller.rb 
def index 
    @categories = Something.all_categories 
    @somethings = Something.select(params[:category]) 
end