2016-05-07 2 views
-2

Любой может помочь решить эту проблему NoMethodError (неопределенный метод` todo_items ')?Получение «неопределенного метода` todo_item »

Это мой файл контроллера:

class Api::TodoItemsController < ApplicationController 
skip_before_filter :verify_authenticity_token 
before_filter :find_todo_list 

def create 

item = @list.todo_items.new(item_params) 
if item.save 
    render status: 200, json: { 
    message: "Successfully created To-do Item.", 
    todo_list: @list, 
    todo_item: item 
    }.to_json 
else 
    render status: 422, json: { 
    message: "To-do Item creation failed.", 
    errors: item.errors 
    }.to_json 
end 
end 

private 

def item_params 
    params.require("todo_item").permit("content") 
end 

def find_todo_list 
    @list = TodoList.find(params[:todo_list_id]) 
end 

конец

Это мой ToDoList Код модели:

class TodoList < ActiveRecord::Base 
end 

Это мой TodoItem Код модели:

class TodoItem < ActiveRecord::Base 
end 

Это эрро r неопределенного метода todo_items:

Started POST "/api/todo_lists/5/todo_items/" for ::1 at 2016-05-07  16:57:45 +0800 
ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations" 
Processing by Api::TodoItemsController#create as JSON 
Parameters: {"content"=>"new", "todo_list_id"=>"5", "todo_item"=>{"content"=>"new"}} 
TodoList Load (0.2ms) SELECT "todo_lists".* FROM "todo_lists" WHERE "todo_lists"."id" = ? LIMIT 1 [["id", 5]] 
Completed 500 Internal Server Error in 21ms (ActiveRecord: 0.6ms) 

NoMethodError (undefined method `todo_items' for # <TodoList:0x007f82e2dbc088>): 
app/controllers/api/todo_items_controller.rb:6:in `create' 

Спасибо.

+0

Пожалуйста, добавьте свой код модели в вашем вопросе – Thorin

+0

уже добавить код модели, пожалуйста, проверьте. – werx

+0

Просто измените ToDoList модель и добавить "HAS_MANY: todo_items" там – Thorin

ответ

0

Вы добавили todo_items в модель TodoList?

has_many: todo_items

Смежные вопросы