Note that there are some explanatory texts on larger screens.

plurals
  1. PORails 3 controller and model subfolders
    primarykey
    data
    text
    <p>I am writing a Rails 3.2.6 app. I want to separate my controllers and models into subfolders to keep my code clean. I have ensured that the model and its subdirectory name are not the same. However, I find that the code fails if the controller subdirectory name and the model subdirectory name are the same.</p> <p>The following application structure works fine:</p> <pre><code>test app controllers postcnt posts_controller.rb models postmdl post.rb </code></pre> <p>but the following structure doesn't:</p> <pre><code>test app controllers postnsp posts_controller.rb models postnsp post.rb </code></pre> <p>When I call the URL:</p> <pre><code>http://localhost:3000/postnsp/posts </code></pre> <p>I get a 500 error with the message:</p> <pre><code>LoadError in Postnsp::PostsController#index Expected /Users/dev/code/test/app/models/postnsp/post.rb to define Postnsp::Post </code></pre> <p>In the above examples I followed the advice here to eliminate model namespacing: <a href="https://stackoverflow.com/questions/1445341/rails-elegant-way-to-structure-models-into-subfolders-without-creating-submodul">Rails: Elegant way to structure models into subfolders without creating submodules</a></p> <p>and added</p> <pre><code>config.autoload_paths += Dir[Rails.root.join('app', 'models', '{**}')] </code></pre> <p>to my application.rb file </p> <p>For the second (failing) example the relevant files are as follows:</p> <p>post.rb:</p> <pre><code>class Post &lt; ActiveRecord::Base attr_accessible :content, :name end </code></pre> <p>posts_controller.rb:</p> <pre><code>class Postnsp::PostsController &lt; ApplicationController def index @posts = Post.all respond_to do |format| format.html # index.html.erb format.json { render json: @posts } end end </code></pre> <p>routes.rb:</p> <pre><code>Test::Application.routes.draw do namespace :postnsp do resources :posts end </code></pre> <p>Can anyone explain why the subdirectories can't be the same? I assume it's something to do with the creation of the postnsp module for the posts_controller.rb but I can't understand why it's preventing the creation of the model. I would like to have consistency in the directory structure naming in both the controller and model folders should I require it.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload