0

我在 rails 中创建了我的第一个 FORM,我希望能够编辑我在 db 中的类别。 _form.html.erb

<%= form_for(@category) do |f| %>
  <% if @category.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@category.errors.count, "error") %> brání v uložení kategorie.</h2>

      <ul>
      <% @category.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

编辑.html.erb

<%= render 'form' %>

<%= link_to 'Show', @category %> |
<%= link_to 'Back', backend_categories_path %>

网址:

http://localhost:3000/backend/categories/1/edit

错误:

undefined method `category_path' for #<#<Class:0x46696f8>:0x3f38478>

路线:

    home_index GET    /home/index(.:format)                  home#index
                 root        /                                      home#index
              contact        /contact(.:format)                     home#contact
         backend_root        /backend(.:format)                     backend/admin#index
   backend_categories GET    /backend/categories(.:format)          backend/categories#index
                      POST   /backend/categories(.:format)          backend/categories#create
 new_backend_category GET    /backend/categories/new(.:format)      backend/categories#new
edit_backend_category GET    /backend/categories/:id/edit(.:format) backend/categories#edit
     backend_category GET    /backend/categories/:id(.:format)      backend/categories#show
                      PUT    /backend/categories/:id(.:format)      backend/categories#update
                      DELETE /backend/categories/:id(.:format)      backend/categories#destroy

我也有新的表单,可以正常呈现 new.html.erb

<%= form_for @category, :url => { :action => "create" }, :html => {:class => "nifty_form"} do |f| %>
  <%= f.text_field :name %>
  <%= f.submit "Vytvořit" %>
<% end %>

所以我有一个问题,为什么编辑表格不起作用,将表格放在单独的文件中有什么好处?谢谢

4

1 回答 1

1

您需要提供/backend/路线的一部分;试试这个:

<%= form_for [:backend, @category] do |f| %>
于 2013-06-13T18:24:31.610 回答