Rails 3 jQuery UI 选项卡问题加载与 Ajax [英] Rails 3 jQuery UI Tabs issue loading with Ajax

查看:38
本文介绍了Rails 3 jQuery UI 选项卡问题加载与 Ajax的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 jQuery UI 选项卡来处理我的 Rails 3 应用程序的选项卡.一个选项卡没有 Ajax,一个选项卡有 Ajax.我是编程和 Rails 的新手,虽然我从 SO 到目前为止我有两个问题:

  1. 单击我的 Ajax 链接会在我的 div 中加载整个网站的布局.我只想加载我的模板.
  2. 无论我查看哪个个人资料,加载的内容仅适用于 current_user 的消息.

这是标签的容器:

<ul id="infoContainer"><li><a href="#tabs-1">关于</a></li><li><%= link_to "Messages", '/profiles/profile_messages', :remote =>真%>/li><div id="tabs-1">

第二个链接是我看到整个网站布局的地方.理想情况下,我想要的是用我的 profile_messages 模板替换 About div.

我的profiles_controller.rb:

def profile_messages@profile = User.find(user.id).profile@messages = User.find(@profile.user_id).messagesresponse_to do |格式|format.html # index.html.erbformat.xml { 渲染:xml =>@消息}结尾结尾

我的profile_messages.html.erb模板:

<% 用于@user.messagse 中的消息 %><div class="message">

<%结束%>

这是我的routes.rb:

匹配/profiles/profile_messages"=>个人资料#profile_messages"资源:配置文件做获取 :profile_messages, :on =>:收藏结尾

我在 application.js 中的 jQuery:

$(function() {$( "#tabs" ).tabs({ajax选项:{错误:函数(xhr,状态,索引,锚){$( 锚点.hash ).html(加载此标签时出错.请重试.");}}});});

我也有profile_messages.js.erb:

$( "#tabs" ).html( "<%= escape_javascript( render(@user.messages) ) %>" );

对正在发生的事情有什么想法吗?

更新:通过将以下内容插入到我的 profile_messages

中,我使布局消失了

def profile_messages@profile = User.find(user.id).profile@messages = User.find(@profile.user_id).messagesresponse_to do |格式|format.html { 渲染:布局 =>零 }format.xml { 渲染:xml =>@消息}结尾结尾

解决方案

问题出在您的控制器上.关于 profile_messages 操作的一些事情:

def profile_messages@profile = User.find(user.id).profile@messages = User.find(@profile.user_id).messagesresponse_to do |格式|format.html # index.html.erbformat.xml { 渲染:xml =>@消息}结尾结尾

1) user.id 来自哪里?您应该使用 ajax 请求发送一些参数,因此除非user"是您控制器中的受保护方法,否则您需要使用 params[:user_id]<提供 find 方法/code> 或类似的东西.

2) 使用 render :layout =>false 修复模板问题.

3) 作为旁注,你的前两行到处都是.这样做会更容易(也更快):

@user = User.includes(:messages, :profile).find(params[:user_id])@messages = @user.messages # 已经在内存中@profile = @user.profile # 已经在内存中

I'm using jQuery UI Tabs to handle tabs my Rails 3 app. One tab is without Ajax, one is with Ajax. I'm new to programming and Rails, and while I got a ton of help from SO thus far I'm having two issues:

  1. Clicking my Ajax link loads the entire site's layout in my div. I want to just load my template.
  2. No matter which profile I view, the content loaded only applies to the current_user's messages.

Here's the container for the tabs:

<div id="tabs">
  <ul id="infoContainer">
    <li><a href="#tabs-1">About</a></li>
    <li><%= link_to "Messages", '/profiles/profile_messages', :remote => true %></li>
  </ul>
  <div id="tabs-1">
  </div>
</div>

The second link is where I see the entire site layout. Ideally what I want is to replace the About div with the my profile_messages template.

My profiles_controller.rb:

def profile_messages
  @profile = User.find(user.id).profile
  @messages = User.find(@profile.user_id).messages
  respond_to do |format|
    format.html # index.html.erb
    format.xml  { render :xml => @messages }
  end
end

My profile_messages.html.erb template:

<div id="tabs-2">
<% for message in @user.messagse %>
  <div class="message">
  </div>
<% end %>
</div>

Here is my routes.rb:

match "/profiles/profile_messages" => "profiles#profile_messages"
resources :profiles do
  get :profile_messages, :on => :collection
end

My jQuery in application.js:

$(function() {
    $( "#tabs" ).tabs({
        ajaxOptions: {
            error: function( xhr, status, index, anchor ) {
                $( anchor.hash ).html(
                    "There was an error loading this tab. Please try again." );
            }
        }
    });
});

I also have profile_messages.js.erb:

$( "#tabs" ).html( "<%= escape_javascript( render(@user.messages) ) %>" );

Any ideas on what's going on?

UPDATE: I got the layout to disappear by inserting the following into my profile_messages

def profile_messages
  @profile = User.find(user.id).profile
  @messages = User.find(@profile.user_id).messages
  respond_to do |format|
    format.html { render :layout => nil }
    format.xml  { render :xml => @messages }
  end
end

解决方案

The problem is in your controller. A few things about the profile_messages action:

def profile_messages
  @profile = User.find(user.id).profile
  @messages = User.find(@profile.user_id).messages
  respond_to do |format|
    format.html # index.html.erb
    format.xml  { render :xml => @messages }
  end
end

1) where is user.id coming from? You should be sending some parameters with the ajax request, so unless 'user' is a protected method in your controller, you'll need to supply the find method with params[:user_id] or something similar.

2) Use render :layout => false to fix the template issue.

3) As a side note, your first two lines are all over the place. It would be easier (and faster) to do this:

@user = User.includes(:messages, :profile).find(params[:user_id])
@messages = @user.messages # already in memory
@profile = @user.profile # already in memory

这篇关于Rails 3 jQuery UI 选项卡问题加载与 Ajax的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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