2016-04-08 3 views
0

私が達成しようとしている目標は、ユーザーがボタンをクリックしてそのページのフィルタを検索するようにすることです。私のページは音楽アーティストに専念していて、ユーザーが「ポップ」を選択すると、そのページは「ポップ」アーティストのみを表示するように更新されます。ここでRails - 新しいテーブルエントリを持つページを更新する

は、私は、アーティストを表示するために使用していますHTML +ルビー:

<% #prints the artists that match the search, prints all for empty search %> 
     <% @artists.each_slice(3) do |artists| %> 
      <div class="row"> 
       <% artists.each do |artist| %> 
        <div class = "artist_images"> 
         <%= link_to image_tag(artist.artist_art, class: 'show_image'), artist_path(artist) %><br/><br/> 
        </div> 
       <% end %> 
      </div> 
     <% end %> 

     <% #prints message showing that the search does not match %> 
     <% if [email protected]? %> 
     <h3><center style = "color: white">There are no artists containing the term(s) "<%= params[:search] %>". Please try 'Adele', 'Drake', 'Kanye West', or 'Taylor Swift'</center></h3> 
     <% end %> 

コントローラは、次のメソッドがあります。

def index 
    @artists = Artist.all 
    end 

    def randomPop 
    @artists = Artist.where(:genre => "\nPop").random(9) 
    end 

誰も私が変更について行くことができる方法を知っています変数@artistsからすべてのアーティストまで、ボタンをクリックするだけでポップスのアーティストに変換できますか?

+0

私はあまりよく分からないただし、Fayeのような別のサーバーを使用して、WebSocketでデータを動的に要求する必要があります(非同期アプリケーション(チャットアプリなど)と同じように)。または、AJAXを使用します。 – EvilTak

+0

ページ全体をAJAXyの方法でリフレッシュまたは実行しますか? –

+0

@Petr Gazarov、ページはかなり小さいので、リフレッシュ全体が細かいでしょう。 – QQPrinti

答えて

2
ビューで

<%= button_to "Pop", artists_path, method: :get, params: { artist_genre: "Pop" } %> 

「自分の状況に基づいて where句であるべき
def index 
    if params[:artist_genre] 
    @artists = Artist.where(:genre => params[:artist_genre]).random(9) 
    else 
    @artists = Artist.all 
    end 
end 
+0

私はこれがおそらく答えだと感じますが、私はまだ何も戻っていません – QQPrinti

+0

心配しないで、問題を見つけました。それは文字列の奇妙な解析でした。大いに感謝する! – QQPrinti

1

この古典的なrailscast episode is on point。私はあなたの検索ボックスをどのように設定するのかを考えてみましょう。あなたは/更新変えるかもしれない唯一のもの:

if search 
    find(:all, :conditions => ['name LIKE ?', "%#{search}%"]) 
else 
    find(:all) 
end 

は、おそらくあなたのコントローラで

if search 
    where("genre = ?", your_genre) 
else 
    all 
end 
関連する問題