在 Rails 3 div 中排除应用程序布局 [英] Exclude application layout in Rails 3 div

查看:41
本文介绍了在 Rails 3 div 中排除应用程序布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在加载一个模板 profile_messages 作为带有 Ajax 的 jQuery UI 选项卡的一部分.但是,当它加载时,profile_messages 继承了我网站的应用程序布局(页眉、页脚等).我尝试将其作为部分来做,但后来事情并没有奏效.所以我想知道是否有办法做到这一点.

我的 application.js 中的 jQuery:

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

Routes.rb:

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

我的profiles#show.html.erb:

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

我的profile_messages 模板:

<% 用于@user.messages 中的消息 %><%结束%>

我的profile_messages.js.erb:

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

我在 profiles_controller.rb 中的 profile_messages 方法:

def profile_messages@profile = User.find(user.id).profile #need to fix 所以@profile 是为每个配置文件定义的,而不仅仅是current_user 配置文件@messages = User.find(@profile.user_id).messagesresponse_to do |格式|format.html # index.html.erbformat.xml { 渲染:xml =>@消息}结尾结尾

那么有什么办法可以解决应用程序布局的问题?

更新:通过将以下内容插入到我的 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 =>@消息}结尾结尾

更新 2: 我尝试渲染部分,但没有奏效.这是我在 profiles#show.html.erb 中更改的内容:

<ul id="infoContainer"><li><a href="#tabs-1">关于</a></li><li><%= link_to render(:partial => 'profiles/profile_messages'), :id =>'qs', :remote =>true do %>Messages&nbsp;</span><% end %></li><div>

然而,这会加载

    中的部分

    解决方案

    在控制器中,在show"动作的末尾添加:

    render :layout =>零

    或者,如果您从其他地方渲染,只需添加 :layout => nil 选项即可渲染.

    更好的解决方案是将其设为部分并调用 render :partial => "profile_messages/profile_message"(如果您在 app/views/profile_messages/_profile_message.html.erb 中有模板 - 请注意文件名中的 _!)

    此外,除了使用escape_javascript,去掉" 引号并使用(render ...).to_json 实际上更简单.

    I'm loading a template profile_messages as part of jQuery UI Tabs with Ajax. However when it loads, profile_messages inherits my site's application layout (header, footer, everything). I tried doing it as a partial but then things didn't work. So I'm wondering if there was a way to do it.

    The jQuery in my 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." );
                }
            }
        });
    });
    

    Routes.rb:

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

    My profiles#show.html.erb:

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

    My profile_messages template:

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

    My profile_messages.js.erb:

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

    My profile_messages method in profiles_controller.rb:

    def profile_messages
      @profile = User.find(user.id).profile #need to fix so @profile is defined for each profile, not just the current_user profile
      @messages = User.find(@profile.user_id).messages
      respond_to do |format|
        format.html # index.html.erb
        format.xml  { render :xml => @messages }
      end
    end
    

    So is there any way to work around the application layout?

    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
    

    UPDATE 2: I tried to render a partial but it didn't work. Here's what I changed in my profiles#show.html.erb:

    <div id="tabs">
      <ul id="infoContainer">
        <li><a href="#tabs-1">About</a></li>
        <li><%= link_to render(:partial => 'profiles/profile_messages'), :id => 'qs', :remote => true do %>Messages<span>&nbsp;</span><% end %></li>
      </ul>
      <div>
      </div>
    </div>
    

    This however loads the partial in <ul id="infoContainer">

    解决方案

    In the controller, add at the end of the "show" action:

    render :layout => nil
    

    Or if you render from somewhere else just add the :layout => nil option to render.

    The better solution would be to make it a partial and call render :partial => "profile_messages/profile_message" (if you have the template in app/views/profile_messages/_profile_message.html.erb - note the _ in the filename!)

    Also instead of using escape_javascript it's actually simpler to ditch the " quotes and use (render ...).to_json.

    这篇关于在 Rails 3 div 中排除应用程序布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆