Rails - 使用新表条目刷新页面

时间:2016-04-08 03:59:24

标签: javascript ruby-on-rails ruby

我想要实现的目标是让用户点击一个按钮,该按钮将刷新到搜索​​的页面过滤器。我的页面专门针对音乐艺术家,并希望一旦用户选择" Pop",页面刷新只显示" pop"艺术家。

以下是我用来展示艺术家的html + ruby​​:

iFILES=/file/path/*.gz
for i in $iFILES;
do gunzip -k $i > /get/in/this/dir/"${i##*/}"
done

控制器具有以下方法:

<% #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 !@artists.present? %>
        <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 更改为pop中的变量?

2 个答案:

答案 0 :(得分:2)

在你看来,

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

在你的控制器中,

def index
  if params[:artist_genre]
    @artists = Artist.where(:genre => params[:artist_genre]).random(9)
  else
    @artists = Artist.all
  end
end

答案 1 :(得分:1)

这个经典railscast episode is on point。我会用它来设置你的搜索框。你唯一可以改变/更新的东西:

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

根据您的情况,可能应该是where条款&#34;

if search
  where("genre = ?", your_genre)
else
  all
end
相关问题