2015-05-18 3 views
2

Я просмотрел много вопросов по SO для этой ошибки, и я не нашел правильный ответ, к сожалению.rails 4 undefined method `_index_path 'при использовании form_for с множественным именем класса

Я создал класс Avis (который по-французски принимает s независимо от того, является ли оно исключительным или множественным.), Используя rails generate scaffold Avis --force-plural.

Как это часть 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' 

Как я могу это исправить?

ответ

2

Avi - это вложенный ресурс Formations. Ваша форма_for должна выглядеть так: form_for(@formation, @avi), которая будет использовать правильный путь form_avis_path.

Для получения дополнительной информации о вложенных ресурсах проверьте Rails Routing from the Outside In

Смежные вопросы