ruby-on-rails 模板错误:缺少部分栏/foo/_new

kpbwa7wx  于 2023-02-26  发布在  Ruby
关注(0)|答案(1)|浏览(197)

我想显示一个根页面,其中显示了一个涡轮增压帧与形式,以创建一个新的对象。然而,路由不工作,因为我想要的,因为我得到了上述错误。

    • 路由. rb**
namespace :cust do
  resources :customer_dates, only: [:index, :show, :create, :new]
end
root "cust/customer_bookings#index"
  • /视图/客户/客户预订/索引. html. erb*
<div><%= render "main" %></div>
  • /视图/客户/客户预订/_ main. html. erb*
<%= turbo_frame_tag @customer_date do %>
  <%= render new_cust_customer_date_path, customer_date: @customer_date %>
<% end %>
    • 错误**

操作视图::模板::错误(缺少{:区域设置=〉[:en],:格式=〉[:html],:变量=〉[],:处理程序=〉[:raw,:erb,:html,:builder,:ruby,:jbuilder ]}的部分客户/客户日期/_ new。
导致错误的行是_main.html.erb中的一行,其中包含new_cust_customer_date_path。在/views/cust/customer_dates文件夹中,我有new.html.erb_form.html.erb视图

hec6srdp

hec6srdp1#

new_cust_customer_date_path是一个url助手,你可以使用它与link_tobutton_toform_with url:等。render与你的本地文件,这可能或可能不匹配的url路径。
澄清:

"localhost:3000/cust/customer_dates/new" # request from the browser
# bin/rails routes                       # will match one of the routes
namespace :cust do                       # `/cust` to `module Cust`
  resources :customer_dates,             # `/customer_dates` to `CustomerDatesController`
    only: [:index, :show, :create, :new] # `/new` to `def new`
end                                      # Cust::CustomerDatesController#new
def new                                  # controller action
  @customer_date = CustomerDate.new      # do some things
                                         # unless you render something, rails
  # render :new                          # will implicitly `render "#{action_name}"`
end                                      # in controllers, `render` defaults to
new.html.erb                             # rendering a template with layout:
                                         # render template: "cust/customer_dates/new", layout: "application"

在视图内部,render默认呈现部分布局,不呈现布局。

new_cust_customer_date_path #=> /cust/customer_dates/new

<%= render "/cust/customer_dates/new", customer_date: @customer_date %>
# which is the same as
<%= render partial: "/cust/customer_dates/new", locals: { customer_date: @customer_date } %>

# partial is the same as template, but will auto prefix `_` to the filename
<%= render template: "/cust/customer_dates/_new", locals: { customer_date: @customer_date } %>

因为你没有_new.html.erb文件,所以你会得到一个错误。如果你需要呈现一个表单,你可以这样做:

<%= render "cust/customer_dates/form", customer_date: @customer_date %>
#                               ^
#                               form is a partial `_form`

# or render `new.html.erb` as a template (no underscore)
#                                         v
<%= render template: "cust/customer_dates/new", locals: { customer_date: @customer_date } %>

注意,"cust/customer_dates/new"只是相对于app/views的路径。

  • 一个月一次 *

相关问题