_form.html.erb中的实例变量不适用于new_path

时间:2014-03-07 16:56:49

标签: ruby-on-rails ruby ruby-on-rails-4

我收到此错误:Admin :: SuperCategories中的NoMethodError #new,undefined method`super_categories_path'。我的路径定义明确,整个逻辑与管理编辑路径一起使用。由于某种原因,它不适用于管理新操作。不知道我在这里做错了什么。

系统管理员/ super_categories / index.html.erb

<%= link_to "Create a new Super Category", 
        new_admin_super_category_path, class: "button success right" %>

系统管理员/ super_categories_controller.rb

def new
    @super_category = SuperCategory.new
end

系统管理员/ super_categories / new.html.erb

<%= render 'form' %>

系统管理员/ super_categories / _form.html.erb

<%= form_for(@super_category) do |f| %>
<div class="field">
    <%= f.label :name %><br>
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :description %><br>
    <%= f.text_area :description, rows: 6 %> 
  </div>
<div class="actions">
  <%= f.submit class: "button success medium"  %>
  </div>
<% end %>

的routes.rb

namespace :admin do
    resources :super_categories
end

resources :super_categories, only: [:show]

1 个答案:

答案 0 :(得分:2)

您忘记了路径中的 admin 部分。您需要让form_for了解它,或者查找super_categories_path而不是admin_super_categories_path

<%= form_for [:admin, @super_category] do |f| %>