【发布时间】:2018-03-19 15:06:47
【问题描述】:
感谢我可以在这里提问。我正在使用带有 ruby 2.5.0 的 rails 5.1.4。 我有两个模型,模型 A 和模型 B。
Model A has_many bs
Model B belongs_to a
管理员用户可以生成新的 a 条目,也可以生成新的 b 条目。 no-admin-user 可以显示、索引或保留。
routes.rb
resources :as, only: [:index, :show] do
resources :bs, only: [:index, :show, :reserve]
end
namespace :admin do
resources :as, only: [:create, :edit, :update, :destroy,
:show] do
member do
post :activate
get :activate
post :deactivate
get :deactivate
end
resources :bs, only: [:create, :edit, :update, :destroy, :show]
end
end
在app/views/admin/as/show.html.erb 中,您可以看到a 的DB 条目的值。所以现在我的想法是意识到您可以使用表单添加 b 的新 B 条目。我试过这个 show.html.erb
show.html.erb
<h2><%=t("show")%></h2>
<p>
<strong><%=t("title")%></strong>
<%= @a.title %>
</p>
<p>
<strong><%=t("description")%></strong>
<%= @a.description %>
</p>
<p>
<strong><%=t("email")%>:</strong>
<%= @a.email %>
</p>
<h2><%=t("entries")%></h2>
<%= render @a.bs %>
<h2><%=t("addentry")%></h2>
<%= form_with(model: [ @a, @admin.bs.build ], local: true) do |f| %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :description %><br>
<%= f.text_area :description %>
</p>
<p>
<%= f.submit %>
</p>
将生成表单,但 html-sourcecode 不显示“admin-route-path”。但是我该怎么做呢?
<form action="/as/1/bs" accept-charset="UTF-8" method="post"
如何更改 form_with 部分,以便我可以从 b 将条目添加到关联模型中?
【问题讨论】:
-
我在发布的 form_with 中有一个错误。我的这段代码的 SC 是 Yes 我知道
nested_attributes_for,但是 -
我在发布的 form_with 中有一个错误。我的这段代码的 SC 不是“@admin”,它是
<%= form_with(model: [ @a, @a.bs.build ], local: true) do |f| %>我像入门指南一样尝试过 guides.rubyonrails.org/… 是的,我知道nested_attributes_for,但是我要在form_with中更改什么? -
并非如此,
form_for已被弃用。 api.rubyonrails.org/v5.1/classes/ActionView/Helpers/… 所以我用form_with试了一下,但是怎么样
标签: ruby-on-rails ruby database web-development-server