【问题标题】:Couldn't find Todo with 'id'找不到带有“id”的待办事项
【发布时间】:2014-09-09 17:47:36
【问题描述】:

大家好,这个问题我已经从高处和低处搜索了一个答案,所以我终于要在这里问了。我是一个尝试学习 html 和 ruby​​ 的新手程序员。我正在编写一个非常简单的待办事项应用程序,但我不知道如何修复此错误:

“找不到 'id'=# 的 Todo”

tod​​os_controller.rb

class TodosController < ApplicationController

def index
  @todos = Todo.all 

end

def new
  @todo = Todo.new
end


def edit
  @todo = Todo.find(params[:id])
end

def create
    @todo = Todo.create(todo_params)
    redirect_to todos_path
end

private 
def todo_params
  params.require(:todo).permit(:item)end


end

这是我的看法

new.html.slim

= form_for (@todo) do |f|
 p
   = f.text_field :item, :placeholder => "Add new item"
 p
   = f.submit

edit.html.slim

= form_for(@link) do |f|
 p
   = f.text_field :item
 p
   = f.submit

index.html.slim

- @todos.each do |todo|
  ul
        li=todo.item 

p
 = link_to "New", new_todo_path
p
 = link_to "Edit", edit_todo_path(@todos)
p
 = link_to "Delete", '#'

routes.rb

Rails.application.routes.draw do
root to: "todos#index"
 resources :todos
end

编辑:

我的编辑视图和现在写的一模一样

= form_for(@todo) do |f|
  p
   = f.text_field :item
  p
   = f.submit

我要离开这个教程http://masteruby.github.io/weekly-rails/2014/03/22/how-to-create-todo-list-app-static-pages.html#.VA8t4_ldU3l

抱歉,如果我的格式有误,第一次在这里提问。

【问题讨论】:

    标签: html ruby-on-rails ruby slim-lang


    【解决方案1】:

    您正在尝试将编辑链接链接到所有@todos,但它应该链接到单个todo

    = link_to "Edit", edit_todo_path(todo)
    

    【讨论】:

      【解决方案2】:

      如果你查看你的 index.html.slim,你需要传递一个 todo 的值而不是所有的 todo:

      // @todos contains a collection of all the todos
      - @todos.each do |todo|
        // inside this block todo contains your single todo
        ul
          li=todo.item 
        p
          = link_to "New", new_todo_path
        p
          = link_to "Edit", edit_todo_path(todo) #notice it's todo not @todos
        p
          = link_to "Delete", '#'
      

      这将解决您的上述错误,但您的 edit.html.slim 中还有另一个错误,因此您需要修复它,否则它会给您另一个错误,在您的表单中使用@todo 而不是@link

      = form_for @todo do |f|
        // fields
      

      此外,如果您想为您的表单显示验证错误,那么在您的创建方法中您应该使用 .save 而不是 .create,签出 Displaying Validation errors in views

      【讨论】:

      • 我认为我的 gems 或 rails 有什么问题?我完全复制了你的代码,它给了我另一个错误“C:/row/todo-list-app/app/views/todos/index.html.slim:22:语法错误,意外的keyword_end,期待')'”-_ -
      • 是的,我会删除之前的,抱歉。
      • @dwagz1313 你能显示完整的错误吗?你在哪一行得到这个
      • C:/row/todo-list-app/app/views/todos/index.html.slim:22:语法错误,意外的keyword_end,期待')'; @output_buffer.safe_concat((""));结尾; @output_buffer ^ C:/row/todo-list-app/app/views/todos/index.html.slim:25: 语法错误,出乎意料的keyword_end,期待')' "提取的源代码(第22行附近):p = link_to "删除", '#'"
      • @dwagz1313 再次,这看起来更像是一个缩进错误,正如我刚刚在我之前的评论中解释过的那样,当您使用 haml 或 slim 时,您不会关闭块。只需尝试确保所有内容都正确缩进
      猜你喜欢
      • 2022-11-23
      • 2015-10-05
      • 2017-06-07
      • 1970-01-01
      • 1970-01-01
      • 2021-05-22
      • 2020-09-02
      • 1970-01-01
      • 2017-07-04
      相关资源
      最近更新 更多