0

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

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

2 Comments

In my application_controller.rb, current_user is protected. I add render :layout => false but it seems to cause my profile_messages to fail to load.
If I do @user = User.includes(:questions, :profile).find(params[:user_id]) in rails console I get: ActiveRecord::RecordNotFound: Couldn't find User with ID=user_id

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.