如何使用 Rails 部分重新加载 div

发布于 2024-12-06 05:06:25 字数 1808 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沐歌 2024-12-13 05:06:25

在您的控制器中:

def ajax_load_events
  respond_to do |format|
    format.js
  end    
end

ajax_load_events.js.erb

$("#load_events").html('<%= escape_javascript(render :partial => "your_partial", :locals => { :events => @events}) %>');

更新:
routes.rb

get "/ajax_load_events" => "your_controller#ajax_load_events"

.js 中的

 $.get("/ajax_load_events/", {}, null, "script");

In your controller:

def ajax_load_events
  respond_to do |format|
    format.js
  end    
end

ajax_load_events.js.erb

$("#load_events").html('<%= escape_javascript(render :partial => "your_partial", :locals => { :events => @events}) %>');

Update:
routes.rb

get "/ajax_load_events" => "your_controller#ajax_load_events"

in your .js

 $.get("/ajax_load_events/", {}, null, "script");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文