应用/控制器/ todos_controller.rb
def add
todo = Todo.create(:todo_item => params[:todo_text])
if !todo.valid?
flash[:error] = todo.errors.full_messages.join("<br>").html_safe
end
redirect_to :action => 'index'
end
错误消息:
TodosController中的NameError #add undefined局部变量或方法 'todo'代表#
如何解决这些问题?
答案 0 :(得分:0)
试试这个:
def add
todo = Todo.create(:todo_item => params[:todo_text])
if todo && !todo.valid?
flash[:error] = todo.errors.full_messages.join("").html_safe
end
redirect_to :action => 'index'
end