I get the error
undefined method `favorite_relationships_path'
when I display this form:
<%= form_for(current_user.favorite_relationships.build(lesson_id: @lesson.id),
remote: true) do |f| %>
<div><%= f.hidden_field :lesson_id %></div>
<%= f.submit "Favorite", class: "btn btn-large btn-primary" %>
<% end %>
I’m not sure why. I have a controller called favorite_relationships_controller.rb and a model file, favorite_relationship.rb, with the code
class FavoriteRelationship < ActiveRecord::Base
attr_accessible :lesson_id
belongs_to :user
belongs_to :lesson
end
My user model also has:
has_many :favorite_relationships
has_many :lessons, :through => :favorite_relationships
I’m really not sure why im getting that error. Help would be appreciated.
Defining controllers, actions and views is not enough. You need to define routes in
config/routes.rbto connect URLs to your controllers/actions. Defining RESTful resources withresources :favourite_relationshipsin your routing file is what causes Rails to generate the*_pathand*_urlhelpers; until you do this there is no way for requests to reach your app, and no way for your app to generate routes based on your models.Your routes file should look something like this:
This generates the typical “CRUD” routes required for a RESTful resource: