我遇到了问题。经过一个小时的搜索,我仍然在解决这个问题。我在这里经历了大多数问题,但没有一个对我有用。感觉就像我错过了一些简单的事情。
这是我的错误
ActionView::Template::Error (undefined method `client_infos_path' for #
. <#<Class:0x007fda7a69b160>:0x007fda7a6994a0>
Did you mean? client_session_path
client_path
clients_path):
1: <h2>Add <%= current_user.username%> info</h2>
2:
3: <%= form_for @client_info do |f| %>
4: <div class="field">
5: <%= f.label :company_size, 'Company Size' %> <br />
6: <%= f.text_field :company_size %>
app/views/client_infos/new.html.erb:3:in
app_views_client_infos_new_html_erb__493536261245691520_70288166673800'
这是我的控制人
class ClientInfosController < ApplicationController
before_action :authorize_user
def new
# binding.pry
@client_info = ClientInfo.new
@client = Client.find(params[:client_id])
end
还有我的routes.rb
resources :clients do
resources :projects
resources :client_infos, only: [:new, :create]
end
提前谢谢
答案 0 :(得分:2)
ActionView :: Template :: Error(未定义的方法“ client_infos_path”用于
您在client_infos
下有clients
的嵌套资源,因此没有适合您的路由。您需要更改
<%= form_for @client_info do |f| %>
到
<%= form_for [@client, @client_info] do |f| %>
提示:
运行rake routes
。它将列出所有可用的路线。
答案 1 :(得分:2)
如果您在控制台中执行rake routes
,则会得到:
client_projects GET /clients/:client_id/projects(.:format) projects#index
POST /clients/:client_id/projects(.:format) projects#create
new_client_project GET /clients/:client_id/projects/new(.:format) projects#new
edit_client_project GET /clients/:client_id/projects/:id/edit(.:format) projects#edit
client_project GET /clients/:client_id/projects/:id(.:format) projects#show
PATCH /clients/:client_id/projects/:id(.:format) projects#update
PUT /clients/:client_id/projects/:id(.:format) projects#update
DELETE /clients/:client_id/projects/:id(.:format) projects#destroy
client_client_infos POST /clients/:client_id/client_infos(.:format) client_infos#create
new_client_client_info GET /clients/:client_id/client_infos/new(.:format) client_infos#new
clients GET /clients(.:format) clients#index
POST /clients(.:format) clients#create
new_client GET /clients/new(.:format) clients#new
edit_client GET /clients/:id/edit(.:format) clients#edit
client GET /clients/:id(.:format) clients#show
PATCH /clients/:id(.:format) clients#update
PUT /clients/:id(.:format) clients#update
DELETE /clients/:id(.:format) clients#destroy
如您所见,您没有client_infos
路径,因为client_infos
嵌套在clients
下。相反,您有client_client_infos
-考虑到您的routes.rb
应该做的。您也可以这样做(如2.9 Creating Paths and URLs From Objects中所述):
<%= form_for [@client, @client_info] do |f| %>
Rails会推断您要使用client_client_infos
命名的路由。