简体   繁体   中英

rails namespace nomethod error

I am working with a few models, including acts , bibliography , authors . Trying to organise routes so that administrative actions on those models are grouped together under namespace administration.

This is my routes.rb:

namespace :administration do
  resources :actes, path: 'actes', path_names: { new: 'nouveau', edit: 'editer' }, except: [:chercher, :voir]
  get 'actes/nouveau/:id' => 'actes#nouveau', as: 'nouvel_acte'
  post 'actes/nouveau/:id' => 'actes#ajouter'
end

My controller has this :

class Administration::ActesController < ApplicationController

  def nouveau
    @titre = "Fons - ajouter un acte"
    @bib = Biblio.find(params[:id])
    @bibid = @bib.id
  end

In nouveau.html.erb I do this :

<% @bib.auteurs.each do |l| %>
  <%= l.nom_complet %>, 
<% end %>
<%= @bib.titre %>, <%= @bib.lieu %>, <%= @bib.annee %>

This provokes a NoMethodError : undefined method 'auteurs' . I turns out that the entire method @bib is not know - although it works fine in rails console.

I see two options here:

  1. @bib object is nil , so you get an undefined method error for NilClass .
  2. You don't have a relation between Bibliography and Auteur classes. You should have a statement in the first class with something like has_many :auteurs .

If you can paste here the whole error message I could be more specific. Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM