Sasha
Sasha

Reputation: 3281

undefined method in rails

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.

Upvotes: 0

Views: 151

Answers (2)

user229044
user229044

Reputation: 239230

Defining controllers, actions and views is not enough. You need to define routes in config/routes.rb to connect URLs to your controllers/actions. Defining RESTful resources with resources :favourite_relationships in your routing file is what causes Rails to generate the *_path and *_url helpers; 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:

MyApp::Application.routes.draw do
  resources :favourite_relationships
end

This generates the typical "CRUD" routes required for a RESTful resource:

favourite_relationships     GET    /favourite_relationships(.:format)          {:action=>"index", :controller=>"favourite_relationships"}
                            POST   /favourite_relationships(.:format)          {:action=>"create", :controller=>"favourite_relationships"}
 new_favourite_relationship GET    /favourite_relationships/new(.:format)      {:action=>"new", :controller=>"favourite_relationships"}
edit_favourite_relationship GET    /favourite_relationships/:id/edit(.:format) {:action=>"edit", :controller=>"favourite_relationships"}
     favourite_relationship GET    /favourite_relationships/:id(.:format)      {:action=>"show", :controller=>"favourite_relationships"}
                            PUT    /favourite_relationships/:id(.:format)      {:action=>"update", :controller=>"favourite_relationships"}
                            DELETE /favourite_relationships/:id(.:format)      {:action=>"destroy", :controller=>"favourite_relationships"}

Upvotes: 1

user825623
user825623

Reputation:

Rails has _path and _url helpers for routes, which are set up in config/routes.rb. You'll need to ensure that you've defined the routes for FavouriteRelationshipController; something like:

resources :favourite_relationships

You can check the routes defined for your application using the rake routes command.

You can find more information about routing at the Rails Routing from the Outside In guide.

Upvotes: 2

Related Questions