Trevor Nederlof
Trevor Nederlof

Reputation: 2716

Friend System Not Working

Hey all, I have been away from rails for a while and have been catching up to rails 3, I think my problem might stem from me using an unsupported syntax from rails 2 that didnt make it to rails 3. Anyways, my problem might be to broad but while I have been able to fix all the other problems with this app, this one still puzzles me.

On the home page of the site there is this code:

<%- unless @requested_contacts.empty? -%>
  <h2>Contact Requests</h2>
  <ul class="list requests small">
    <%- @requested_contacts.each do |contact| -%>
      <%- conn = Connection.conn(current_user, contact) -%>
      <li>

        <p><%= person_link contact %></p>
        <p class="meta published">
          <%= time_ago_in_words(conn.created_at) %> 
          ago
        </p>
        <p>
          <%= link_to "Respond", edit_connection_path(conn) %> |
          <%= link_to "View profile", contact %>
        </p>
      </li>
    <%- end -%>
  </ul>
<%- end -%>

When I click respond instead of getting to the Connection edit page, I get an error:

undefined method `contact' for nil:NilClass

app/controllers/connections_controller.rb:15:in `edit'

{"id"=>"4"}

The controller code is:

 def edit    
    @contact = @connection.contact
  end

The relavent models are, Person.rb:

  has_many :connections

  has_many :contacts, :through => :connections, :order => 'people.created_at DESC'
  has_many :requested_contacts, :through => :connections, :source => :contact

Connection.rb:

  belongs_to :person
  belongs_to :contact, :class_name => "Person", :foreign_key => "contact_id"

  def conn(person, contact)
    find_by_person_id_and_contact_id(person, contact)
  end

The edit page I am trying to get to is:

<h2>Contact request</h2>

<p>You have a contact request from <%= @contact.name %></p>

<% form_for(@connection) do |f| %>
  <p>
    <%= f.submit "Accept", :class => "button" %>
    <%= f.submit "Decline", :id => nil, :class => "button" %>
  </p>
<% end %>

I think that is all the relevant code to this issue. If there is anything else you might need to see please let me know, I am very grateful for any an all help. Sorry if it is glare lying obvious, I am learning ruby and rails as a hobby (and loving it so far!).

Upvotes: 0

Views: 128

Answers (3)

Jorge Israel Pe&#241;a
Jorge Israel Pe&#241;a

Reputation: 38616

The problem is that you seem to have mixed things up. In your controller's edit method, you reference @connection which hasn't been instantiated as far as we know, like zetetic said, so it's nil, then you try to call a method on it. It seems we need even more information.


I believe your def conn(person, contact) should be a class method: def self.conn(person, contact), based on the way you are calling it (not from an object but directly from the class).

Basically, the problem is that conn is coming up as nil, I believe it's for the reason I mentioned above, but if not, then figuring out why will find you your answer.

EDIT: If you are still getting the same error, then I recommend you try this out in the rails console.

Connection.find_by_person_id_and_contact_id(person, contact)

Try and use the same information you are using in the form which is generating the error. Most likely the problem is that there is no connection between the person and the contact. If you are sure there is but the above method says otherwise, then there is most likely an issue with how you set up your associations. Remember that not only do you have to specify the association in the class but you also have to modify the tables accordingly to reflect the association.

Upvotes: 1

zetetic
zetetic

Reputation: 47548

When you see this error:

undefined method `contact' for nil:NilClass

It usually means you're trying to call a method on an unassigned variable. Since the error points to the controller, and the code you've shown us is:

def edit    
  @contact = @connection.contact
end

I'm going to guess that @connection is unassigned. Normally you would expect to have a finder method to retrieve the value before using it, like:

@connection = Connection.find(params[:id])

Sometimes this code is placed in a before_filter, to reduce duplication. If you do have such a filter, perhaps it is not being reached. It's hard to know without seeing the rest of the controller code.

Upvotes: 2

Nikita Barsukov
Nikita Barsukov

Reputation: 2984

Assuming your associations are OK, and person references a connection.

Replace this line <%- conn = Connection.conn(current_user, contact) -%> with <%- conn = @person.conn(current_user, contact) -%>

Upvotes: 0

Related Questions