title: Ruby on Rails: How to pass dynamically-assigned link IDs to controller method
body: <p>Sorry if I mess something up, but I'm new here, so bear with me.</p>
  
  <p>Here's kinda what I want to do. I want to dynamically assign ID tags to links based on the user ID's in my database. Then based on which link that I click, I want that link to call the controller helper and pass in its ID to the helper method.</p>
  
  <p>So far, I'm able to dynamically assign each link for each user, but my problem is when I click the link and it calls the controller method <strong>callThis</strong> with the ID, it gets the last user's (the last user in the database) ID instead of the user ID of the link that I clicked.</p>
  
  <p>My question is, how do I get it to send the ID of the link that I click, and not the ID of the last user in the database?</p>
  
  <p>Here's my code. <br/><br/>
  <strong>index.html.erb</strong></p>
  
  <pre><code>&lt;div id="profile"&gt;&lt;/div&gt;
  &lt;table&gt;
  &lt;% @users.each do |u| %&gt;
      &lt;% if !u.nil? and u.department_id == department.id %&gt;
        &lt;tr class="&lt;%= department.id %&gt;" style="display:none;text-align:left;"&gt;
          &lt;td&gt;
            &lt;a href="#id-&lt;%= u.id %&gt;" id="id-&lt;%= u.id %&gt;" onclick="&lt;%= callThis(u.id) %&gt;; refresh();"&gt;
              &lt;%= "#{u.first_name} #{u.last_name}" %&gt; 
            &lt;/a&gt;
          &lt;/td&gt;
        &lt;/tr&gt;
      &lt;% end %&gt; 
    &lt;% end %&gt;
  &lt;/table&gt;
  &lt;script&gt;
    function refresh(){
      $('#profile').html("&lt;%= escape_javascript(render(:partial =&gt; 'profile')) %&gt;"); 
    }
  &lt;/script&gt;
  </code></pre>
  
  <p></p>
  
  <p><strong>professors_controller.rb</strong></p>
  
  <pre><code>class ProfessorsController &lt; ApplicationController
    helper_method :callThis
    helper_attr :get_id
    attr_accessor :get_id
  
    def index
      @departments = Department.all
      @users = User.with_role :Professor
      @search  = @users.search(params[:q])
      @result = @search.result
      @id = 0
    end
  
    def callThis(id)
      @id = id
    end
    def get_id
      @id
    end
  end
  </code></pre>
  
  <p><strong>profile.html.erb</strong></p>
  
  <pre><code>&lt;%= get_id.to_s %&gt;
  </code></pre>