1

I have the following:

Routes:

.
.
.
resources :users do
  resources :relationships
end

new.html.erb:

<section id="main">

  <%= form_for @relationship do |f| %> #This is the line the error is on
    <div class="field">
      <%= f.label :name %>
      <%= f.text_field :name %>
    </div>
   <div class="actions"><%= f.submit %></div>
   <% end %>
</section>

relationships_controller.rb

class RelationshipsController < ApplicationController

  def new
      @id = params[:user_id]
      @rel_user = User.find_by_id(params[:user_id])
      @relationship = Relationship.new
  end

  def create

  end

end

relationship.rb #model

class Relationship < ActiveRecord::Base
    belongs_to :user

    # TYPES = ['Family', 'Friend', 'Spouse']
end

I've hunted around on Stack Overflow and can't seem to find the answer, although, I think it has something to do with nesting my resources. I get the following error:

undefined method 'relationships_path' for #<#<Class:0x007ff45f15ff80>:0x007ff45f15bc78>

Any ideas?

2
  • 1
    you've nested the relationship ressources so the path change. As per your previous question, run rake routes to get the solution. Commented Oct 28, 2011 at 18:50
  • @apneadiving so I see I need to use: new_user_relationship but isn't the issue with this line: @relationship = Relationship.new in my controller? What am I missing? Commented Oct 28, 2011 at 18:59

1 Answer 1

5

You should understand that all '_path' helpers generated from route.rb file. So in your case route will generate this helper users_relationship_path for show action.

But in your form you're using just form_for @relationship which is expected to use relationship_path helper.

So you should tell your form helper to use nesting, like this:

<%= form_for [@rel_user, @relationship]  do |f| %>
Sign up to request clarification or add additional context in comments.

5 Comments

I made users plural because everything else was, but are you saying that if a user has many relationships then it should be singular?
It depends on how many users you have, maybe you're right so I fixed my answer.
Do you just need an instance of user and relationship to pass into the form? Or why do I use @rel_user and not @current_user? I've tested them and they both seem to work.
@NoahClark: There's no reason to use one or the other. All that is required for the first element in that array is an object that is of the User class. The second object has to be an object of the Relationship class.
@NoahClark, I get '@rel_user' from your code, you better know what is a user. As I understood you have several users and for every of them you want to have the relationships.

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.