1

我是新来的狂欢和红宝石。在我的 spree 应用程序中创建自定义控制器时,我可以使用 deface 在 spree 管理面板中成功添加指向它的链接。但是当我转到该链接时,它给了我以下错误

NoMethodError in Spree::Admin::Societies#new
Showing app/views/spree/admin/societies/_form.html.erb where line #1 raised:
undefined method `societies_path' for #<#<Class:0x007f19cb636898>:0x007f19c5ecacf8>

我不知道它从哪里寻找“societies_path”,因为我已经更新了 app/views/spree/admin/societies/new.html.erb 来寻找“admin_societies_path”,这里是

<%= render 'form' %>
<%= link_to 'Back', admin_societies_path %>

和 app/views/spree/admin/societies/_form.html.erb 包含

    <%= form_for(@society) do |f| %>
     <% if @society.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@society.errors.count, "error") %> prohibited this society from being saved:</h2>
      <ul>
      <% @society.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :name %><br>
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :address %><br>
    <%= f.text_field :address %>
  </div>
  <div class="field">
    <%= f.label :area %><br>
    <%= f.text_field :area %>
  </div>
  <div class="field">
    <%= f.label :postcode %><br>
    <%= f.number_field :postcode %>
  </div>
  <div class="field">
    <%= f.label :city %><br>
    <%= f.text_field :city %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
    <% end %>

我也尝试删除返回链接,但它再次给出相同的错误。

config/routes.rb 是

mount Spree::Core::Engine, :at => '/'
Spree::Core::Engine.add_routes do
 namespace :admin do
    resource :societies
  end
end

我的 app/controllers/spree/admin/societies_controller.rb 是

module Spree
 module Admin
 class SocietiesController < Spree::Admin::BaseController
  before_action :set_society, only: [:show, :edit, :update, :destroy]

  def index
    @societies = Society.all
  end

  def show
  end

  def new
    @society = Society.new
  end

  def edit
  end

  def create
    @society = Society.new(society_params)

    respond_to do |format|
      if @society.save
        format.html { redirect_to @society, notice: 'Society was successfully created.' }
        format.json { render :show, status: :created, location: @society }
      else
        format.html { render :new }
        format.json { render json: @society.errors, status: :unprocessable_entity }
      end
    end
  end
  def update
    respond_to do |format|
      if @society.update(society_params)
        format.html { redirect_to @society, notice: 'Society was successfully updated.' }
        format.json { render :show, status: :ok, location: @society }
      else
        format.html { render :edit }
        format.json { render json: @society.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @society.destroy
    respond_to do |format|
      format.html { redirect_to societies_url, notice: 'Society was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    def set_society
      @society = Society.find(params[:id])
    end

    def society_params
      params.require(:society).permit(:name, :url, :building_number, :address, :area, :postcode, :city, :active, :IsDelete)
    end
end
end
end

任何帮助将不胜感激。

4

1 回答 1

1

我怀疑这是 _form partial 中的这一行

<%= form_for(@society) do |f| %>

您需要在此处引用名称空间,所以可能类似于

<%= form_for([:admin, @society]) do |f| %>

或添加您自己的网址

<%= form_for(@society, url: admin_societies_path) do |f| %>
于 2015-09-18T13:26:08.037 回答