rails 4 undefined方法`_index_path'当使用具有复数类名的form_for时

时间:2015-05-18 14:44:37

标签: ruby-on-rails ruby plural

我已经浏览了很多关于此错误的问题,但遗憾的是我没有找到正确答案。

我已经使用Avis创建了课程s(使用法语,rails generate scaffold Avis --force-plural无论是单数还是复数。{ / p>

作为Formation课程的一部分,这里是route.rb文件(部分):

resources :formations do
    resources :avis
end

这是Avis控制器:

class AvisController < ApplicationController
  before_action :set_avi, only: [:show, :edit, :update, :destroy]
  before_action :set_formation

  #On indique que les tests sont fait sur l'autorisation des utilisateurs
  load_and_authorize_resource :formation

  # gestion du layout
  layout :sections_layout
  @layout = 'back'

  respond_to :html

  def sections_layout
    @layout
  end

  def index
    @avis = Avis.where(:formations_id => Formation.find_by(:id => formation_params))
    respond_with(@avis)
  end

  def show
    respond_with(@avi)
  end

  def new
    @avi = Avis.new
    respond_with(@formation, @avi)
  end

  def edit
  end

  def create
    @avi = Avis.new(avis_params)
    @avi.save
    respond_with(@avi)
  end

  def update
    @avi.update(avis_params)
    respond_with(@avi)
  end

  def destroy
    @avi.destroy
    respond_with(@avi)
  end

  private
    def set_avi
      @avi = Avis.find(params[:id])
    end

    def avis_params
      params[:avi]
    end

    def formation_params
    params.require(:formation_id)
    end

    def set_formation
        @formation = Formation.find_by(:id => params[:formation_id])
        if @formation == nil
          redirect_to forbidden_path :status => 403
        end
      end
end

当我尝试创建新的Avis时,我收到此错误:

ActionView::Template::Error (undefined method `avis_index_path' for #<#<Class:0x007ffa6052aa78>:0x007ffa60528b88>):
    1: <% puts @avi%>
    2: 
    3: <%= form_for(@avi) do |f| %>
    4:   <% if @avi.errors.any? %>
    5:     <div id="error_explanation">
    6:       <h2><%= pluralize(@avi.errors.count, "error") %> prohibited this avi from being saved:</h2>
  app/views/avis/_form.html.erb:3:in `_app_views_avis__form_html_erb__481767065103572634_70356711120220'
  app/views/avis/new.html.erb:3:in `_app_views_avis_new_html_erb___1382100742020377330_70356626529020'
  app/controllers/avis_controller.rb:29:in `new'

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

AviFormations的嵌套资源,您的form_for应该看起来像form_for(@formation, @avi),它将使用正确的formation_avis_path。

有关嵌套资源的详细信息,请查看Rails Routing from the Outside In