您好我是rails的新手并试图找出如何构建搜索以从Soundcloud获取信息。
如果我将所有内容构建为静态,那么我会收回信息,但我无法使用搜索表单
控制器中的方法:
def search
@client = Soundcloud.new(:client_id => ENV["SOUNDCLOUD_CLIENT_ID"])
end
这项工作
#search.html.erb
<div class="form">
<% @client.get('/tracks', :q => 'djsneak' ).each do |track| %>
<ul>
<li><%= track.title %></li>
<li><%= image_tag track.artwork_url %></li>
</ul>
<% end %>
</div>
我想用什么:
<!--search.html.erb-->
<div class="form">
<%= form_tag("soundcloud_search", method: "get") do %>
<%= label_tag(:q, "Search for:") %>
<%= text_field_tag(:q) %>
<%= submit_tag("Search") %>
<% end %>
</div>
<div>
<h3>results</h3>
<ul>
<li><%= track.title %></li>
<li><%= image_tag track.artwork_url %></li>
</ul>
</div>
答案 0 :(得分:0)
我自己解决了这个问题,但对于谁感兴趣:
<!--search.html.erb-->
<!--search form-->
<div class="form">
<%= form_tag("searchit", method: "get") do %>
<%= label_tag(:q, "Search for:") %>
<%= text_field_tag(:q, params[:q]) %>
<%= submit_tag("Search", :name => nil) %>
<% end %>
</div>
<!--Results-->
<div class="form">
Search Results
<% @client.get('/tracks', :q => params[:q] ).each do |track| %>
<ul>
<li><%= track.title %></li>
<li><%= image_tag track.artwork_url %></li>
</ul>
<% end %>
</div>