Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails rails表单中未定义的方法接触路径_Ruby On Rails - Fatal编程技术网

Ruby on rails rails表单中未定义的方法接触路径

Ruby on rails rails表单中未定义的方法接触路径,ruby-on-rails,Ruby On Rails,我的路线定义如下 get 'contacts/new', to: 'contacts#new' def new @contact = Contact.new end 在我的ContactsController中,我定义如下 get 'contacts/new', to: 'contacts#new' def new @contact = Contact.new end 在views contacts/new/_form.html.erb中,我构建了如下表单 <%= form

我的路线定义如下

get 'contacts/new', to: 'contacts#new'
def new
  @contact = Contact.new
end
在我的ContactsController中,我定义如下

get 'contacts/new', to: 'contacts#new'
def new
  @contact = Contact.new
end
在views contacts/new/_form.html.erb中,我构建了如下表单

<%= form_for @contact, html: {multipart:true} do |f|  %>

<%= f.label :username %>
<%= f.text_field :username %>

<% end %>

当我在routes文件中定义了rails时,你知道为什么rails会抛出这样的错误吗。我只是想了解rails路由的内部工作原理

为了避免此类错误,请删除路由并使用:

resources :contacts, only: [:new, :create]

要避免此类错误,请删除路由并使用:

resources :contacts, only: [:new, :create]

尽量使用railsy-way,如@Graham提到的
resources


尽量使用railsy-way,如@Graham提到的
resources


为联系人创建一个post类型的路由(这是一个抛出错误)

或者取消这条路线

 get 'contacts/new', to: 'contacts#new'
简单地加上这个

resources :contacts

为联系人创建一个post类型的路由(这是一个抛出错误)

或者取消这条路线

 get 'contacts/new', to: 'contacts#new'
简单地加上这个

resources :contacts

将控制器返回到
get'contacts/new',返回到:'contacts#new'
,然后在控制台上键入:
rake routes
。然后将路由更改为
get'contacts/new',更改为:'contacts#new',如:'contact'
,然后再次键入
rake routes
。比较结果将控制器返回到
获取'contacts/new',返回到:'contacts#new'
,然后在控制台上键入:
rake routes
。然后将路由更改为
get'contacts/new',更改为:'contacts#new',如:'contact'
,然后再次键入
rake routes
。比较结果