0

サブレット(アパート)のリストを表すために使用しているリストモデルがあり、ユーザーが自分の好みに合わせてリストをフィルター処理する方法としてフィルターモデルを作成しました。ここに私のFilterクラスがあり、フォーム送信に基づいてリストをクエリするためのメソッドリストがあります。

class Filter < ActiveRecord::Base
  attr_accessible :air_conditioning, :available_rooms, :bathrooms, :furnished,  :negotiable, :new, :parking, :maximum_price, :private_bathroom, :show, :term, :total_rooms, :utilities, :washer_dryer
  serialize :term

  def listings
    @listings ||=find_listings
  end

private

  def find_listings
    listings=Listing.order(:price)
    listings=listings.where("listings.price <= ?", maximum_price) if maximum_price.present?
    listings=listings.where(total_rooms: total_rooms) if total_rooms.present?
    listings=listings.where(available_rooms: available_rooms) if available_rooms.present?
    listings=listings.where(bathrooms: bathrooms) if bathrooms.present?
    listings=listings.where(term: term)
    listings=listings.where(furnished: furnished)
    listings=listings.where(negotiable: negotiable)
    listings=listings.where(utilities: utilities)
    listings=listings.where(air_conditioning: air_conditioning)
    listings=listings.where(parking: parking)
    listings=listings.where(washer_dryer: washer_dryer)
    listings=listings.where(private_bathroom: private_bathroom)
    listings
  end

end

これらを表示するために、部分的なテンプレートをレンダリングするフィルターのshowメソッドを作成しました。これは私が現在持っているものですが、/listingsに_listings.html.erbという名前のテンプレートを作成しません。

<p id="notice"><%= notice %></p>

<%= @filter.listings.size %>

<%= render @filter.listings  %>

そしてここにテンプレートがあります

<div style="padding:5px">
<%= link_to 'New Listing', new_listing_path,{:style=>'', :class => "btn"} %>
<h1>Available Sublets</h1>

<table id="listingTable" class="table table-bordered table-hover">
  <tr>
    <th><%= link_to 'Filter', new_filter_path,{:style=>'', :class => "btn"} %><%= link_to 'Clear Filter', listings_path, {:style=>'', :class => "btn"} %></th>
    <th>Address</th>
    <th><u><%= "Price Per Month" %></u></th>
    <th>Description</th>
  </tr>
<% if @listings !=nil %>
    <% @listings.each do |listing| %>
      <tr onmouseover="this.style.cursor='pointer';"
      onclick="window.location.href = '<%= url_for(:controller => 'listings', :action => 'show', :id=>listing.id) %>' " >
        <td><%= image_tag listing.photo.url(:small) %></td>
        <td><%= listing.address %></td>
        <td>$<%= listing.price %></td>
        <td width="40%"><%= listing.description %></td>
      </tr>
    <% end %>
<% end %>
<% else if @listings==nil %>
    <p> Sorry, No Sublets Fit Your Criteria! </p>
<% end %>
</table>

私の命名規則はめちゃくちゃだと思いますが、これを行う正しい方法を見つけることができません。誰にでも提案があります。また、フィルターは常に空になっているようです。単純なフィルターで何度もテストしましたが、機能しません。誰かが私のフィルターにエラーを見つけたら、遠慮なくそれを指摘してください。ありがとう!

4

1 に答える 1

1

renderを呼び出して配列またはリレーションを渡すと、実際にはパーシャルの単数バージョンが呼び出されます。電話するとき

<%= render @filter.listings %>

最終的には、次のようになります。

<%= render :partial => 'listings/listing', :collection => @filter.listings %>

これは

<% @filter.listings.each do |listing| %>
  <%= render listing %>
<% end %>

また、 `<%end%> <%else if ...%>にエラーがあるか、部分的にエラーが発生している場合は、次のようになります。

<% if @listings != nil %>
    <% @listings.each do |listing| %>
      <tr onmouseover="this.style.cursor='pointer';"
      onclick="window.location.href = '<%= url_for(:controller => 'listings', :action => 'show', :id=>listing.id) %>' " >
        <td><%= image_tag listing.photo.url(:small) %></td>
        <td><%= listing.address %></td>
        <td>$<%= listing.price %></td>
        <td width="40%"><%= listing.description %></td>
      </tr>
    <% end %>
<% else %> (since you already checked if it was nil, you it is implied at this point the value is nil
    <p> Sorry, No Sublets Fit Your Criteria! </p>
<% end %>

Ruby風なので、次のように書くことをお勧めします。

<% if @listings.blank? %>
    <p> Sorry, No Sublets Fit Your Criteria! </p>
<% else %>
     <% @listings.each do |listing| %>
      <tr onmouseover="this.style.cursor='pointer';"
      onclick="window.location.href = '<%= url_for(:controller => 'listings', :action => 'show', :id=>listing.id) %>' " >
        <td><%= image_tag listing.photo.url(:small) %></td>
        <td><%= listing.address %></td>
        <td>$<%= listing.price %></td>
        <td width="40%"><%= listing.description %></td>
      </tr>
    <% end %>
<% end %>

私が最終的にこのケースを処理する方法は次のとおりです。

フィルタ表示ビュー

<p id="notice"><%= notice %></p>

<%= @filter.listings.size %>
<div style="padding:5px">
<%= link_to 'New Listing', new_listing_path,{:style=>'', :class => "btn"} %>
<h1>Available Sublets</h1>

<table id="listingTable" class="table table-bordered table-hover">
  <tr>
    <th><%= link_to 'Filter', new_filter_path,{:style=>'', :class => "btn"} %><%= link_to 'Clear Filter', listings_path, {:style=>'', :class => "btn"} %></th>
    <th>Address</th>
    <th><u><%= "Price Per Month" %></u></th>
    <th>Description</th>
  </tr>
  <%= render(@filter.listings) ||  "<p> Sorry, No Sublets Fit Your Criteria! </p>".html_safe %>
</table>

'listings / _listing.html.erb'

<tr onmouseover="this.style.cursor='pointer';"
  onclick="window.location.href = '<%= url_for(listing) %>' " >
  <td><%= image_tag listing.photo.url(:small) %></td>
  <td><%= listing.address %></td>
  <td>$<%= listing.price %></td>
  <td width="40%"><%= listing.description %></td>
</tr>
于 2013-03-03T18:54:09.980 に答える