Rails 3 模板未在 jQuery UI 选项卡中加载
我的 Rails 3 应用程序中有一个配置文件。当查看个人资料时,个人资料的有关信息默认显示在 div
容器中。我想要做的是,当单击“消息”链接时,将 div
中的“关于”信息替换为来自用户的消息。 (消息应根据 profile_messages
模板加载。)为此,我转向了 jQuery UI 选项卡。
所以“关于”信息存在于 Tab-1 中。 “消息”位于名为 profile_messages
的模板中,我尝试从 ProfilesController 和 MessagesController 加载该模板。
我在这里得到了很多帮助才能到达我现在的位置。但是,由于某种原因,我无法显示实际的消息。当我单击“消息”时会发生什么,我在 jQuery 中看到错误消息。如果我检查该元素,我会看到以下内容:
Failed to load resource: the server responded with a status of 404 (Not Found) `profile_messages`
这是我的代码。如果有人可以帮助我弄清楚发生了什么,我将非常感激。
< strong>ProfilesShow show.html.erb:
<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><!-- end profile-about -->
profile_messages.html.erb
:
<div id="tabs-2">
<% for 'message' in @user.messages %>
<div class="message-1">
</div>
</div><!-- end messages -->
<% end %>
ProfilesController
:
def show
@profile = Profile.find(params[:id])
@user = User.find(@profile.user_id)
end
Routes.rb
:
resources :messages do
resources :responses
end
resource :messages do
collection do
get :profile_messages
end
end
profile_messages.js.erb
:
$( "#tabs" ).html( "<%= escape_javascript( render(@profile.messages) ) %>" );
来自rake 路线
:
message GET /messages/:id(.:format) {:action=>"show", :controller=>"messages"}
profile GET /profiles/:id(.:format) {:action=>"show", :controller=>"profiles"}
profile_messages_messages GET /messages/profile_messages(.:format {:action=>"profile_messages", :controller=>"messages"}
jQuery UI 选项卡:
$(function() {
$( "#tabs" ).tabs({
spinner: '<img src="">' },{
ajaxOptions: {
error: function( xhr, status, index, anchor ) {
$( anchor.hash ).html(
"There was an error loading this tab. Please try again." );
}
}
});
});
正如我所说,我真的很接近,所以如果有人可以帮助我找出我做错了什么,我将不胜感激
更新 。 :如果我从profiles/profile_messages
到 messages/profile_messages
这里是 jQuery UI 选项卡的 HTML 输出:
<div id="tabs">
<ul id="infoContainer">
<li><a href="#tabs-1">About</a></li>
<li><a href="messages/profile_messages" data-remote="true">Messages</a></li>
</ul>
<div id="tabs-1">
这是我在 Firebug 中看到的内容:
<div id="tabs" class="ui-tabs ui-widget ui-widget-content ui-corner-all">
<ul id="infoContainer" class="ui-tabs-nav ui-helper-reset ui-helper-clearfix ui-widget-header ui-corner-all">
<li class="ui-state-default ui-corner-top ui-tabs-selected ui-state-active"><a href="#tabs-1">About</a></li>
<li class="ui-state-default ui-corner-top"><a href="#ui-tabs-1" data-remote="true">Messages</a></li>
</ul>
<div id="tabs-1" class="ui-tabs-panel ui-widget-content ui-corner-bottom">
</div>
</div>
Firebug 将第二个链接显示为 # ui-tabs-1 对我来说似乎很奇怪。
I have a Profile in my Rails 3 app. When the Profile is viewed, the Profile's about information shows in a div
container by default. What I want to do is replace the "about" information in the div
with messages from the User when a "messages" link is clicked. (The messages should load according to the profile_messages
template.) To do this, I turned to jQuery UI Tabs.
So the "about" information exists in Tab-1. The "Messages" is in a template called profile_messages
that I've tried loading from both the ProfilesController and the MessagesController.
I've gotten a lot of help here on SO to get to where I'm at. However, for some reason I can't get the actual messages to show up. What happens when I click "messages is I see the error message in my jQuery. If I inspect the element, I see this:
Failed to load resource: the server responded with a status of 404 (Not Found) `profile_messages`
Here is my code. If anyone can help me figure out what's going on I'd appreciate it greatly.
ProfilesShow show.html.erb:
<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><!-- end profile-about -->
profile_messages.html.erb
:
<div id="tabs-2">
<% for 'message' in @user.messages %>
<div class="message-1">
</div>
</div><!-- end messages -->
<% end %>
ProfilesController
:
def show
@profile = Profile.find(params[:id])
@user = User.find(@profile.user_id)
end
Routes.rb
:
resources :messages do
resources :responses
end
resource :messages do
collection do
get :profile_messages
end
end
profile_messages.js.erb
:
$( "#tabs" ).html( "<%= escape_javascript( render(@profile.messages) ) %>" );
From rake routes
:
message GET /messages/:id(.:format) {:action=>"show", :controller=>"messages"}
profile GET /profiles/:id(.:format) {:action=>"show", :controller=>"profiles"}
profile_messages_messages GET /messages/profile_messages(.:format {:action=>"profile_messages", :controller=>"messages"}
jQuery UI Tabs:
$(function() {
$( "#tabs" ).tabs({
spinner: '<img src="">' },{
ajaxOptions: {
error: function( xhr, status, index, anchor ) {
$( anchor.hash ).html(
"There was an error loading this tab. Please try again." );
}
}
});
});
As I said, I'm really close so if anyone can help me figure out what I'm doing wrong I'd appreciate it.
UPDATE: If I switch the route from profiles/profile_messages
to messages/profile_messages
here is the HTML output of the jQuery UI Tabs:
<div id="tabs">
<ul id="infoContainer">
<li><a href="#tabs-1">About</a></li>
<li><a href="messages/profile_messages" data-remote="true">Messages</a></li>
</ul>
<div id="tabs-1">
Here is what I see in Firebug:
<div id="tabs" class="ui-tabs ui-widget ui-widget-content ui-corner-all">
<ul id="infoContainer" class="ui-tabs-nav ui-helper-reset ui-helper-clearfix ui-widget-header ui-corner-all">
<li class="ui-state-default ui-corner-top ui-tabs-selected ui-state-active"><a href="#tabs-1">About</a></li>
<li class="ui-state-default ui-corner-top"><a href="#ui-tabs-1" data-remote="true">Messages</a></li>
</ul>
<div id="tabs-1" class="ui-tabs-panel ui-widget-content ui-corner-bottom">
</div>
</div>
The fact that Firebug shows the second link as #ui-tabs-1 seems odd to me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
路由输出说明了一切:profile_messages_messages,但如果您使用生成的路径变量而不使用字符串路径,则还需要附加 _path。
The routes output says it all: profile_messages_messages, but you also need to append _path--if you were using the generated path variable and not using a string path.
这条路线存在吗?
这就是您从 link_to 调用的内容。从您的路线输出来看,这似乎应该是:
Does this route exist?
This is what you are calling from your link_to. From your routes output, it seems this should be:
感谢大家的共同努力,我才得以实现这一目标。查看这些问题以获取更多代码:
排除 Rails 3 div 中的应用程序布局
<一href="https://stackoverflow.com/questions/7693012/rails-3-jquery-ui-tabs-issue-loading-with-ajax/7698207#comment9363399_7698207">Rails 3 jQuery UI 选项卡使用 Ajax 加载时出现问题
Thanks to the combined efforts of folks here on SO I was able to get this to work. Check out these questions for more code:
Exclude application layout in Rails 3 div
Rails 3 jQuery UI Tabs issue loading with Ajax