gpt4 book ai didi

ruby-on-rails - "_form.erb.html"部分不适用于创建和更新表单代码生成

转载 作者:太空宇宙 更新时间:2023-11-03 16:23:17 24 4
gpt4 key购买 nike

我按照 Rails 入门教程稍作修改(在模型和属性的名称上),但我发现使用表单部分有问题。

我的“_form.erb.html”如下所示:

<%= form_for :company do |f| %>
<% if @company.errors.any? %>
<div id="error_explanation">
<h2>
<%= pluralize(@company.errors.count, "error") %> evitaron que la compañía se almacenara
</h2>
<ul>
<% @company.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="input">
<%= f.label :name, "Nombre" %>
<%= f.text_field :name, :required => true%>
</div>
<div class="input">
<%= f.label :handle, "Handle" %>
<%= f.text_field :handle, :required => true%>
</div>
<div class="button">
<%= f.submit "Guardar" %>
</div>
<% end %>

我的 edit.html.erb 看起来像这样:

<h1>Editar empresa</h1>
<hr>
<%= render 'form' %>
<%= link_to 'Regresar al listado', companies_path %>

new.html.erb 看起来像这样:

<h1>Nueva empresa</h1>
<hr>
<%= render 'form' %>
<%= link_to 'Regresar al listado', companies_path %>

之前没有部分,我使用以下代码作为表单标题:

new.html.erb
<%= form_for :company, url: companies_path do |f| %>

edit.html.erb
<%= form_for :company, url: company_path(@company), method: :patch do |f| %>

现在我正在尝试使用部分我得到以下异常:

No route matches [POST] "/companies/new"

Rails.root: /../app

╔═══════════════════╦══════════════════════════╦═══════════════════════════════╦═══════════════════╗
║ Helper ║ HTTP Verb ║ Path ║ Controller#Action ║
╠═══════════════════╬══════════════════════════╬═══════════════════════════════╬═══════════════════╣
║ Path / Url ║ ║ ║ ║
║ companies_path ║ GET ║ /companies(.:format) ║ companies#index ║
║ POST ║ /companies(.:format) ║ companies#create ║ ║
║ new_company_path ║ GET ║ /companies/new(.:format) ║ companies#new ║
║ edit_company_path ║ GET ║ /companies/:id/edit(.:format) ║ companies#edit ║
║ company_path ║ GET ║ /companies/:id(.:format) ║ companies#show ║
║ PATCH ║ /companies/:id(.:format) ║ companies#update ║ ║
║ PUT ║ /companies/:id(.:format) ║ companies#update ║ ║
║ DELETE ║ /companies/:id(.:format) ║ companies#destroy ║ ║
║ pages_index_path ║ GET ║ /pages/index(.:format) ║ pages#index ║
║ root_path ║ GET ║ / ║ pages#index ║
╚═══════════════════╩══════════════════════════╩═══════════════════════════════╩═══════════════════╝

关于如何使用部分的任何想法?

最佳答案

成功

<%= form_for @company do |f| %>

代替

<%= form_for :company do |f| %>

来自Getting Started with Rails指南:

Everything except for the form_for declaration remained the same. The reason we can use this shorter, simpler form_for declaration to stand in for either of the other forms is that @article is a resource corresponding to a full set of RESTful routes, and Rails is able to infer which URI and method to use.

关于ruby-on-rails - "_form.erb.html"部分不适用于创建和更新表单代码生成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30021987/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com