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:
- Clicking my Ajax link loads the entire site's layout in my
div. I want to just load my template. - 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