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>

「admin_societies_path」を探すために app/views/spree/admin/societies/new.html.erb を既に更新しているため、「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| %>

または独自の URL を追加

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