form_for - 访问视图中的控制器变量时出错 (rails 3)

发布于 2024-11-29 02:16:17 字数 1092 浏览 0 评论 0原文

我有一个非常简单的模型/视图,但由于某种原因,我似乎无法访问新的记录变量,因此出现错误:

undefined method `hash_for_checklists_path' for # Module:<0x00000100f8b9b0>

我已经在这个问题上头撞墙几个小时了。谁能看到我做错了什么吗?

控制器

class ChecklistsController < ApplicationController
  def new
    @title = "New Checklist"
    @checklist = Checklist.new 
  end
[...]
end

(顺便说一句,我的 application.html.erb 文件获取 @title 变量没有问题。)

视图(new.html.erb)

<%= form_for @checklist do |f| -%>

Routes.rb

  devise_for :users

  resources :checklist_item_categories, :as => 'item_categories' do
    resources :checklist_items
  end

  resources :checklist_categories do
    resources :checklists
  end  

  match 'checklists/new', :to => 'checklists#new'

  #pages
  get "pages/home"
  get "pages/contact"

  #checklist items
  get "checklist_items/new"

  #checklists
  get "checklists/new"
  get "checklists/edit"
  get "checklists/show"
  get "checklists/index"

  #categories
  get "abstract_categories/new"

I have a very simple model/view but for some reason I can't seem to access the new record variable and therefore get the error:

undefined method `hash_for_checklists_path' for # Module:<0x00000100f8b9b0>

I've hit my head against a wall on this for several hours. Can anyone see what I'm doing wrong?

Controller

class ChecklistsController < ApplicationController
  def new
    @title = "New Checklist"
    @checklist = Checklist.new 
  end
[...]
end

(incidentally my application.html.erb file has no problem getting the @title variable.)

View (new.html.erb)

<%= form_for @checklist do |f| -%>

Routes.rb

  devise_for :users

  resources :checklist_item_categories, :as => 'item_categories' do
    resources :checklist_items
  end

  resources :checklist_categories do
    resources :checklists
  end  

  match 'checklists/new', :to => 'checklists#new'

  #pages
  get "pages/home"
  get "pages/contact"

  #checklist items
  get "checklist_items/new"

  #checklists
  get "checklists/new"
  get "checklists/edit"
  get "checklists/show"
  get "checklists/index"

  #categories
  get "abstract_categories/new"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十二 2024-12-06 02:16:17

您没有表单中帖子的路径。

也许可以

resources :checklists

代替

#checklists
get "checklists/new"
get "checklists/edit"
...

You don't have a path for the post from the form.

Maybe put

resources :checklists

in place of

#checklists
get "checklists/new"
get "checklists/edit"
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文