form_for 方法未定义 [英] form_for method undefined

查看:59
本文介绍了form_for 方法未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读了一本名为Rails 3 in Action"的书,做了两页:index"和new",设置 routes.rb:

I read a book called 'Rails 3 in Action' and made two pages: 'index' and 'new', set routes.rb:

root :to => 'projects#index'
match '/new', to:'projects#new'

和projects_controller:

and projects_controller:

def new
  @project = Project.new
end

def create
  @project = Project.new(parmas[:project])
  @project.save
  flash[:notice] = "Project has been created"
  redirect_to @project
end

并查看文件:

index.html.erb

index.html.erb

<%= link_to "new", new_path %>

这工作正常,因为我最终在 localhost:3000/new,但问题是:

This works correctly, because I end up at localhost:3000/new, but the problem is:

<%= form_for(@project) do |f| %>

结果:

未定义的方法`projects_path' #<#:0x416b830>

undefined method `projects_path' for #<#:0x416b830>

projects_path 在哪里?当我打印 <%= root_path %> 时,我得到 /,但是 <%= projects_path %> 给出了错误未定义的方法.

Where is projects_path? When I print <%= root_path %>, I get /, but <%= projects_path %> gives error undefined method.

如何定义方法projects_path?根不是projects_path?

How do I define a method projects_path? Root is not projects_path?

推荐答案

你应该在 routes.rb 中为项目定义资源

You should define resource for project in routes.rb

resources :projects

这将生成帮助程序 projects_path 和一组 others

This will generate helper projects_path and a banch of others

这篇关于form_for 方法未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆