3

I've browse a lot of question over SO for this error and I didn't find the correct answer unfortunately.

I've created the class Avis (which, in French, takes an s no matter if it's singular or plural.) using rails generate scaffold Avis --force-plural.

As it's part of the Formation class, here's the route.rb file (part of):

resources :formations do
    resources :avis
end

Here's the Avis controller:

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

When I try to create a new Avis I get this error:

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'

How can I fix this?

1 Answer 1

3

Avi is a nested resource of Formations your form_for should look like this form_for(@formation, @avi), which will use the correct formation_avis_path.

For more information on nested resources check out Rails Routing from the Outside In

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.