扶手:通过Ajax的渲染谐音 [英] Rails: Rendering Partials through Ajax

查看:109
本文介绍了扶手:通过Ajax的渲染谐音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个标签页,我只想通过AJAX呈现不同的谐音,当选项卡后,点击。我有一些code,但我不认为这是最有效的方式。我希望如果有人能帮助我与我的code表示已经到位,或者有更简单的方法,我想AP preciate了很多。谢谢!

HTML标签

 <李类='StreamTab StreamTabRecent主动>
<%=的link_to'最近',mostrecent_schools_path,:远程=>如此,:类=> TabText%>
< /李>


< D​​IV ID ='ContentBody'>
< D​​IV ID ='阿贾克斯'>< / DIV>
<%=渲染用户/ microposts,:microposts => @microposts%>
< / DIV>
 

学校控制器

 类SchoolsController<的ApplicationController
  高清mostrecent
    @school = School.find_by_slug(request.url.gsub('的http://本地主机:3000 /','')).ID
    @microposts = @ user.microposts.order('created_at DESC)分页(:per_page => 3,:页=> PARAMS [:页面])。
     respond_to代码做|格式|
      的format.html
      format.js
     结束
  结束
结束
 

最新JS

  $(#ContentBody)HTML('<%= escape_javascript(渲染:部分=>中的用户/ microposts)%>')。
 

路线

 资源:学校办
    集合做
        得到:mostrecent
    结束
  结束
 

解决方案

请参阅我的回答到previous职位。这会给你一个想法,<一个href="http://stackoverflow.com/questions/9154874/ajax-in-rails-show-modelnew-form-after-completing-a-modelupdate/9156082#9156082">AJAX在Rails的:在完成一个模型#更新

后显示模式#新形式

你可以做的就是呈现在行动JSON,并通过部分为JSON。

 高清mostrecent
  @school = School.find_by_slug(request.url.gsub('的http://本地主机:3000 /','')).ID
  @microposts = @ user.microposts.order('created_at DESC)分页(:per_page =&GT; 3,:页=&GT; PARAMS [:页面])。
  respond_to代码做|格式|
    format.json {渲染:JSON =&GT; {:成功=&GT;如此,:HTML =&GT; (render_to_string用户/ microposts')}}
    的format.html {}
  结束
结束
 

您可以访问您的js的JSON(即一个JavaScript / CoffeeScript的文件,如mostrecent.js或mostrecent.js.coffee),并取代目前的元素。

  $('。TabText')活'的ajax:成功',(事件数据) - &GT;
  $('#ContentBody)。HTML(data.html)如果(data.success ==真)
 

I have a page with tabs and I want to just render different partials through ajax when the tabs are clicked upon. I have some code but I don't think it is the most efficient way. I was hoping if anyone can help me with my code that is already in place or if there is an easier method I would appreciate it a lot. Thank you!

HTML Tab

<li class='StreamTab StreamTabRecent active'>
<%= link_to 'Most Recent', mostrecent_schools_path, :remote => true, :class => 'TabText' %>
</li>


<div id='ContentBody'>
<div id='ajax'></div>
<%= render 'users/microposts', :microposts => @microposts %>
</div>

School Controller

class SchoolsController < ApplicationController  
  def mostrecent
    @school = School.find_by_slug(request.url.gsub('http://localhost:3000/','')).id
    @microposts = @user.microposts.order('created_at DESC').paginate(:per_page => 3, :page => params[:page])
     respond_to do |format|
      format.html
      format.js
     end
  end
end

Most Recent JS

$("#ContentBody").html('<%= escape_javascript(render :partial => "users/microposts" )%>');

Routes

  resources :schools do
    collection do
        get :mostrecent
    end
  end

解决方案

See my answer to a previous post. This will give you an idea AJAX in Rails: Show model#new form after completing a model#update

What you can do is render a json in the action and pass the partial as json.

def mostrecent
  @school = School.find_by_slug(request.url.gsub('http://localhost:3000/','')).id
  @microposts = @user.microposts.order('created_at DESC').paginate(:per_page => 3, :page => params[:page])
  respond_to do |format|
    format.json { render :json => {:success => true, :html => (render_to_string 'users/microposts')} }
    format.html { }
  end
end

You can access the json in your js (i.e. a javascript/coffeescript file such as mostrecent.js or mostrecent.js.coffee) and replace the current element.

$('.TabText').live 'ajax:success', (event,data) ->
  $('#ContentBody').html(data.html) if(data.success == true)

这篇关于扶手:通过Ajax的渲染谐音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆