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

标签 ruby-on-rails ruby

我按照 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/

相关文章:

ruby-on-rails - 如何在 Rails 中管理后台服务器池

ruby-on-rails - rails 中的 send_file 不会下载到浏览器 - 而是直接在浏览器窗口中加载

ruby - 如何重复使用随机字母

ruby - 为什么 Ruby 中的后缀 `if` 工作如此奇怪

ruby - 如何将 Ruby 的 STDIN 传递给 Open3.popen3 调用的外部程序?

ruby-on-rails - flag_shih_tzu 可以处理的最大标志数量是多少?

ruby-on-rails - Rails-枚举字段的测试验证

ruby-on-rails - 直接上传到 S3 并调整图像大小

ruby-on-rails - 设置 Capistrano 当前路径

ruby-on-rails - Paperclip - 他们如何创建附加到文件名的时间戳?