【问题标题】:Call a partial from AJAX URL Rails从 AJAX URL Rails 调用部分
【发布时间】:2012-09-18 23:15:16
【问题描述】:

如何从 Ajax 函数调用部分内容,我需要在 home.html.erb 中呈现部分内容,这里是代码:

<div class="span8">
  <h3>Micropost Feed</h3>
    <div class="posts">
        <%= render 'feed' %>
    </div>
</div>

在底部我有一个脚本,问题代码在这些行中:

if(nearBottomOfPage()) {
  loading=true;
  page++;
  $.ajax({ // send an ajax-request to load the next page
    url: '/feed?page=' + page,
    type: 'get',
    dataType: 'script',
    success: function() { // after successfully completing the ajax-request redraw the    sausage-navigation.
      $(window).sausage('draw');
      loading=false;
    }
  });

“url”参数是我不知道要写什么来调用部分的地方,这个部分是调用 _feed.html.erb 并且它位于 Home.html.erb 的同一文件夹中,最后在我的控制器操作中我有这个:

def home
  if signed_in?
    @post  = current_user.posts.build
    @feed_items = current_user.feed.order(:created_at).page(params[:page])
  end  
  respond_to do |format|
      format.js  { render :partial => 'feed'}
      format.html # index.html.erb
      format.xml  { render :xml => @feed_items }
    end
 end

如何从 Ajax 函数调用该部分?

非常感谢。

【问题讨论】:

    标签: ruby-on-rails ajax partial-views


    【解决方案1】:

    您不能通过 URL 直接调用部分。只有 /public 目录中的页面/视图可以直接访问。

    您需要创建一个控制器操作来呈现“feed”部分,以及相应的路由。然后使用该路由的路径作为您的 AJAX URL。

    【讨论】:

    • 然后我在 feed.html.erb 有我需要的内容,如何通过 escape_javascript(????) 渲染它:s
    猜你喜欢
    • 1970-01-01
    • 2011-06-13
    • 2014-07-16
    • 2021-10-16
    • 2013-06-19
    • 2011-11-09
    • 1970-01-01
    • 1970-01-01
    • 2013-03-15
    相关资源
    最近更新 更多