错误模板:缺少部分 bar/foo/_new

huangapple go评论65阅读模式
英文:

Template Error: missing partial bar/foo/_new

问题

我想显示一个根页面,其中显示一个带有创建新对象表单的涡轮帧。但是路由不按我希望的方式工作,因为我遇到了上面提到的错误。

routes.rb

namespace :cust do
  resources :customer_dates, only: [:index, :show, :create, :new]
end
root "cust/customer_bookings#index"

/views/cust/customer_bookings/index.html.erb

<div><%= render "main" %></div>

/views/cust/customer_bookings/_main.html.erb

<%= turbo_frame_tag @customer_date do %>
  <%= render new_cust_customer_date_path, customer_date: @customer_date %>
<% end %>

错误

ActionView::Template::Error (Missing partial cust/customer_dates/_new with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :jbuilder]}.

引发错误的是_main.html.erb中的那一行,其中包含了new_cust_customer_date_path。在/views/cust/customer_dates文件夹中,我有new.html.erb_form.html.erb视图。

英文:

I want to display a root page which shows a turbo frame with a form for creating a new object. However the routing doesn't work as I want, because I get the above mentioned error.

routes.rb

namespace :cust do
  resources :customer_dates, only: [:index, :show, :create, :new]
end
root &quot;cust/customer_bookings#index&quot;

/views/cust/customer_bookings/index.html.erb

&lt;div&gt;&lt;%= render &quot;main&quot; %&gt;&lt;/div&gt;

/views/cust/customer_bookings/_main.html.erb

&lt;%= turbo_frame_tag @customer_date do %&gt;
  &lt;%= render new_cust_customer_date_path, customer_date: @customer_date %&gt;
&lt;% end %&gt;

Error

> ActionView::Template::Error (Missing partial cust/customer_dates/_new with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :jbuilder]}.

And the line causing the error is the one in _main.html.erb with new_cust_customer_date_path.
In /views/cust/customer_dates folder I have the views new.html.erb and _form.html.erb

答案1

得分: 2

new_cust_customer_date_path 是一个 URL 辅助工具,您可以在 link_tobutton_toform_with url: 等方法中使用它。render 用于处理您的本地文件,这些文件可能与 URL 路径匹配,也可能不匹配。

为了澄清:

"localhost:3000/cust/customer_dates/new" # 来自浏览器的请求
# bin/rails routes                       # 将匹配其中一个路由
namespace :cust do                       # `/cust` 映射到 `module Cust`
  resources :customer_dates,             # `/customer_dates` 映射到 `CustomerDatesController`
    only: [:index, :show, :create, :new] # `/new` 映射到 `def new`
end                                      # Cust::CustomerDatesController#new
def new                                  # 控制器操作
  @customer_date = CustomerDate.new      # 执行一些操作
                                         # 除非您显式地渲染某些内容,否则 Rails
  # render :new                          # 会隐式地 `render "action_name"`
end                                      # 在控制器中,`render` 默认为
new.html.erb                             # 使用布局渲染模板:
                                         # 渲染模板:"cust/customer_dates/new",布局:"application"

在视图中,render 默认用于渲染局部视图,不使用布局。

new_cust_customer_date_path #=> /cust/customer_dates/new

<%= render "/cust/customer_dates/new", customer_date: @customer_date %>
# 与下面的代码相同
<%= render partial: "/cust/customer_dates/new", locals: { customer_date: @customer_date } %>

# 局部视图与模板相同,但会自动在文件名前添加下划线 `_`
<%= render template: "/cust/customer_dates/_new", locals: { customer_date: @customer_date } %>

由于没有 _new.html.erb 文件,您会收到错误。如果需要渲染一个表单,可以这样做:

<%= render "cust/customer_dates/form", customer_date: @customer_date %>
#                               ^
#                               表单是局部视图 `_form`

# 或者将 `new.html.erb` 作为模板渲染(没有下划线)
#                                         v
<%= render template: "cust/customer_dates/new", locals: { customer_date: @customer_date } %>

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


https://guides.rubyonrails.org/layouts_and_rendering.html

英文:

new_cust_customer_date_path is a url helper, you can use it with link_to, button_to, form_with url: etc. render works with your local files, which may or may not match the url path.

To clarify:

&quot;localhost:3000/cust/customer_dates/new&quot; # 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 &quot;#{action_name}&quot;`
end                                      # in controllers, `render` defaults to
new.html.erb                             # rendering a template with layout:
                                         # render template: &quot;cust/customer_dates/new&quot;, layout: &quot;application&quot;

Inside the views, render defaults to rendering a partial and no layout.

new_cust_customer_date_path #=&gt; /cust/customer_dates/new

&lt;%= render &quot;/cust/customer_dates/new&quot;, customer_date: @customer_date %&gt;
# which is the same as
&lt;%= render partial: &quot;/cust/customer_dates/new&quot;, locals: { customer_date: @customer_date } %&gt;

# partial is the same as template, but will auto prefix `_` to the filename
&lt;%= render template: &quot;/cust/customer_dates/_new&quot;, locals: { customer_date: @customer_date } %&gt;

Since you don't have _new.html.erb file, you get an error. If you need to render a form, you can do this:

&lt;%= render &quot;cust/customer_dates/form&quot;, customer_date: @customer_date %&gt;
#                               ^
#                               form is a partial `_form`

# or render `new.html.erb` as a template (no underscore)
#                                         v
&lt;%= render template: &quot;cust/customer_dates/new&quot;, locals: { customer_date: @customer_date } %&gt;

Note that &quot;cust/customer_dates/new&quot; is just a path relative to app/views.


https://guides.rubyonrails.org/layouts_and_rendering.html

huangapple
  • 本文由 发表于 2023年2月24日 02:13:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75548760.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定