James Sweet
James Sweet

Reputation: 129

Nested resource missing _url

I am trying to write a simple api using ruby and rails and have almost got it working, I am just having one issue. When I try and create a nested resource, I receive an "undefined method" error.

I have two models, one is a user model and one is a record model. They are setup in the routes.rb as such:

resources :users, :except => [:new,:edit] do
  resources :records, :except => [:new,:edit]
end

My records controller contains the following:

def show
  respond_with @user.records.find(params[:id])
end

def create
  respond_with @user.records.create(params[:record])
end

When I call show with a parameter, it works and returns the model. When I post a create I get the following error:

NoMethodError: undefined method `record_url' for #<RecordsController:0x007fea3682a5c8>

I am not sure why this is occuring but if anyone has any ideas I would be grateful. Also, if any more code is required to help, tell me so that I can add it.

Forgot to add my rake routes:

user_records GET    /users/:user_id/records(.:format)     records#index
             POST   /users/:user_id/records(.:format)     records#create
 user_record GET    /users/:user_id/records/:id(.:format) records#show
             PUT    /users/:user_id/records/:id(.:format) records#update
             DELETE /users/:user_id/records/:id(.:format) records#destroy
       users GET    /users(.:format)                      users#index
             POST   /users(.:format)                      users#create
        user GET    /users/:id(.:format)                  users#show
             PUT    /users/:id(.:format)                  users#update
             DELETE /users/:id(.:format)                  users#destroy

Upvotes: 0

Views: 253

Answers (1)

Muhammet
Muhammet

Reputation: 326

@record =  @user.records.create(params[:record])
respond_with(@user, @record)

Upvotes: 1

Related Questions