如何使用 Rails 部分重新加载 div
Rails 有没有办法用部分
控制器
def home
@events = []
end
def ajax_load_events
...
@band = Band.find_by_name(@artist)
begin
artist = Bandsintown::Artist.new({
:name => @artist
})
@events = artist.events
rescue Bandsintown::APIError
@events = [] #this will ensure that if an error happens from the bandsintown api, we'll catch the error
end
end
HTML
<div class="field disable_color">
<label for="search">Select a Song</label>
<input type="search" name="request[song]" id="request_song" value="" />
</div>
<tbody class="load_events">
<%= render :partial => 'event', :collection => @events %>
</tbody>
部分
<% @events.each_with_index do |event, index| %>
<tr class="gradeU">
<td><%= check_box_tag "instance_selected#{index}", "2"%></td>
<td><%= event.datetime.strftime("%B %d, %Y") %></td>
<td><%= "#{event.venue.name} - #{event.venue.city}, #{event.venue.country}" %></td>
</tr>
<% end %>
jQuery
$("#request_song").autocomplete({
source: function(req, add){
$.getJSON('/song_ajax/trackName/'+ $("#request_artist").val() + '', req, function(data) {
var suggestions = data.suggestions;
add(suggestions);
});
},
change: function() {
var artist_url = encodeURIComponent($('#request_artist').val().replace(/\./g, ""));
$.getJSON('/ajax_load_events/'+ artist_url + '', function(data) {
});
},
});
重新加载 div基本上我需要使用填充有 ajax_load_events 操作的 @events 数组刷新 div #load_events ...我缺少什么想法
is there a way in rails to reload a div with a partial
Controller
def home
@events = []
end
def ajax_load_events
...
@band = Band.find_by_name(@artist)
begin
artist = Bandsintown::Artist.new({
:name => @artist
})
@events = artist.events
rescue Bandsintown::APIError
@events = [] #this will ensure that if an error happens from the bandsintown api, we'll catch the error
end
end
HTML
<div class="field disable_color">
<label for="search">Select a Song</label>
<input type="search" name="request[song]" id="request_song" value="" />
</div>
<tbody class="load_events">
<%= render :partial => 'event', :collection => @events %>
</tbody>
partial
<% @events.each_with_index do |event, index| %>
<tr class="gradeU">
<td><%= check_box_tag "instance_selected#{index}", "2"%></td>
<td><%= event.datetime.strftime("%B %d, %Y") %></td>
<td><%= "#{event.venue.name} - #{event.venue.city}, #{event.venue.country}" %></td>
</tr>
<% end %>
jQuery
$("#request_song").autocomplete({
source: function(req, add){
$.getJSON('/song_ajax/trackName/'+ $("#request_artist").val() + '', req, function(data) {
var suggestions = data.suggestions;
add(suggestions);
});
},
change: function() {
var artist_url = encodeURIComponent($('#request_artist').val().replace(/\./g, ""));
$.getJSON('/ajax_load_events/'+ artist_url + '', function(data) {
});
},
});
Basically i need the div #load_events be refreshed with the @events array that is populated with the ajax_load_events action ...any ideas what i am missing
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的控制器中:
ajax_load_events.js.erb
更新:
routes.rb
.js 中的
In your controller:
ajax_load_events.js.erb
Update:
routes.rb
in your .js