Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8296303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T14:56:24+00:00 2026-06-08T14:56:24+00:00

I have a nav menu with 2 tabs/links in the show.html.erb file, in UsersController.rb

  • 0

I have a nav menu with 2 tabs/links in the show.html.erb file, in UsersController.rb, I would like to use ajax to render different partial for the tabs.

In the show.html.erb I have a div named profile-data where I want to show the content.
So I do something like this:

The link structure:

<li><%= link_to "College friends", college_friends_path, :remote => true %></li>
<li><%= link_to "Highschool friends", highschool_friends_path, :remote => true %></li>

I define the routes:

match "college_friends" => "users#college_friends", :as => "college_friends"
match "highschool_friends" => "users#highschool_friends, :as => "highschool_friends"

And I define in my UserController.rb the necessary methods:

class UsersController < ApplicationController
  def show
    @user = User.find(params[:id])
  end

  def college_friends
    respond_to do |format|
      format.js
    end
  end

  def highschool_friends
    respond_to do |format|
      format.js
    end
  end
end

Last thing we have the JS files:

*college_friends.js.erb*

$('#profile-data').html("<%= escape_javascript(render(:partial => 'college_friends')) %>");

*highschool_friends.js.erb*

$('#profile-data').html("<%= escape_javascript(render(:partial => 'highschool_friends')) %>");

The partial code: _college_friends.html.erb

<% groups = @user.friends.group_by(&:college_name) %>
<% sorted_groups = groups.sort_by{|key, values| values.count}.reverse %>
<% sorted_groups.each do |collegename, friends| %>
<% next if collegename.blank? %>
<div class="contentbox">
    <div class="box-header">
        <h3><%= collegename %></h3>
        <div class="meta-info">
            <p><i class="icon-map-marker"></i> Malmö</p>
            <p><i class="icon-user"></i><span class="count"> <%= friends.count %></span> vänner</p>
        </div>
    </div>
    <ul class="friends-list">
        <% friends.map do |friend| %>
        <li><%= image_tag(friend.image) %>
            <% end %> 
        </ul> 
    </div>
<% end %>

Nothing happens when I click the the links, and get this error in the console:

Started GET "/universitet_friends" for 127.0.0.1 at 2012-07-29 01:53:39 +0200
Processing by UsersController#universitet_friends as JS
  Rendered users/_universitet_friends.html.erb (1.6ms)
  Rendered users/universitet_friends.js.erb (3.1ms)
Completed 500 Internal Server Error in 7ms

ActionView::Template::Error (undefined method `friends' for nil:NilClass):
    1: <% groups = @user.friends.group_by(&:college_name) %>
    2: <% sorted_groups = groups.sort_by{|key, values| values.count}.reverse %>
    3: <% sorted_groups.each do |collegename, friends| %>
    4: <% next if collegename.blank? %>
  app/views/users/_universitet_friends.html.erb:1:in `_app_views_users__universitet_friends_html_erb___1983680250475155079_70236040373720'
  app/views/users/universitet_friends.js.erb:1:in `_app_views_users_universitet_friends_js_erb__1317362850668628869_70236044930260'
  app/controllers/users_controller.rb:19:in `universitet_friends

Any help would be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T14:56:26+00:00Added an answer on June 8, 2026 at 2:56 pm

    ActionView::Template::Error (undefined method `friends’ for
    nil:NilClass):

    This is telling you that your @user variable is nil. This happened because when you went back to your controller for the AJAX request, you never actually set the @user variable. It does not persist between requests. You need to pass that variable during the ajax request. One way to do it is to add a user_id param to the ajax URL.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a nav menu with 2 tabs/links in the show.html.erb file, in UsersController.rb
I have nav code like this in my HTML file: <div id="center_links"> <ul> <li><a
I have a problem. I would like to use the animate() function to slide
I have this horizontal menu. The menu looks like tabs and are 29px high.
I have a nav menu for which I want to use a lava lamp
I have this nav menu with 2 tabs: <div id=user_menu> <ul class=nav nav-tabs> <li
Ok, very very weird issue here. I have a sub nav menu that links
I have a menu set up this way: <div id='top-navigation'> <ul> <li><a class='top-nav-button-unclicked' href='#'>SUBJECT</a></li>
I have a nav button added by myself. when click it I use: $(#+gridId).setColProp('myColumn',
My goals: I want to have #nav to be divide into menu-left and menu-right,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.