英文:
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 "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 %>
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_to
、button_to
、form_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:
"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"
Inside the views, render
defaults to rendering a partial and no layout.
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 } %>
Since you don't have _new.html.erb
file, you get an error. If you need to render a form, you can do this:
<%= 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 } %>
Note that "cust/customer_dates/new"
is just a path relative to app/views
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论