title: Sinatra: NameError at '/route', undefined local variable or method
body: <p>I'm trying to build an admin backend for a cms website. </p>
  
  <p>this is the structure of my application</p>
  
  <pre><code>├── app.rb
  ├── Gemfile
  ├── models
  │   └── models.rb
  ├── routes
  │   └── routes.rb
  └── views
      ├── categories.erb
      ├── # ... other view files
  </code></pre>
  
  <p><strong>app.rb</strong> </p>
  
  <pre><code>require 'sinatra'
  require 'data_mapper'
  require 'dm-core'
  require 'dm-migrations'
  require 'digest'
  
  enable :sessions
  
  DataMapper.setup(:default, 'mysql://username:password@localhost/database')
  require './models/models.rb' 
  require './routes/routes.rb'
  DataMapper.finalize
  </code></pre>
  
  <p><strong>models.rb</strong></p>
  
  <pre><code>class Category
    include DataMapper::Resource
  
    property :id,         Serial
    property :name,       String
  
    has n, :posts
  end
  # other model definitons
  </code></pre>
  
  <p>I've defined a <code>categories</code> route inside my <strong>routes.rb</strong></p>
  
  <pre><code>...
  get '/categories' do 
      @categories = Category.all
      erb :categories
  end 
  ...
  </code></pre>
  
  <p>Contents of view (<code>categories.erb</code>) file. </p>
  
  <pre><code>                #table headers   
                  &lt;tbody&gt;
                  &lt;% @categories.each do |c| %&gt;
                      &lt;tr&gt;
                          &lt;td&gt;
                              &lt;%= c.id %&gt;
                          &lt;/td&gt;
                          &lt;td&gt;
                              &lt;%= c.name %&gt;
                          &lt;/td&gt;
                          &lt;td&gt;
                              &lt;%= с.posts.count %&gt;
                          &lt;/td&gt;
                          &lt;td&gt;
                              &lt;%= c.posts(:order =&gt; [:updated_at.desc]).first.updated_at.strftime("%d/%m/%Y") %&gt;
                          &lt;/td&gt;
                      &lt;/tr&gt;
                  &lt;% end %&gt;
                  &lt;/tbody&gt;
  </code></pre>
  
  <p>When I browse to <code>/categories</code> route, server spits this error</p>
  
  <pre><code>NameError at /categories
  undefined local variable or method `с' for #&lt;Sinatra::Application:0x0000000284bc08&gt;
  </code></pre>
  
  <p>I have not faced such problem before. And really don't know what is going on. </p>
  
  <p>The problem probably is not with application structure (the require sequences inside app.rb) because, before <code>get '/categories'</code> I've defined a <code>post '/login'</code> route which checks user inputs against database records. And it works as I want.</p>
  
  <pre><code>post '/login' do 
      email = params[:email]
      pwd = params[:password]
  
      user = Author.first(:email=&gt;email)
  
      if user.nil?
          redirect to ('/register')
      elsif !user.nil?
          if Digest::MD5.hexdigest(pwd) == user.password
          ... #and so on
  </code></pre>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I'm listing other table/models with same methods, and all of them works as i expected, but categories. </p>
  
  <pre><code>other routes  
  get '/articles' do 
      @articles = Post.all(:is_blog_post =&gt; false)
      erb :site_articles
  end
  ##blog articles
  get '/blogposts' do 
      @barticles = Post.all(:is_blog_post =&gt; true)
      erb :blog_articles
  end
  #users
  get '/admin_users' do
      @admins = Author.all(:is_admin=&gt;true)
      erb :admin_users 
  end
  
  #bloggers
  get '/bloggers' do
      @bloggers = Author.all(:is_admin=&gt;false)
      erb :blog_users 
  end
  </code></pre>
  
  <p>and corresponding view files
  <strong>site articles</strong></p>
  
  <pre><code>            &lt;tbody&gt;
              &lt;% @articles.each do |art| %&gt;
                  &lt;tr&gt;
                      &lt;td&gt;
                          &lt;%= art.title %&gt;
                      &lt;/td&gt;
                      &lt;td&gt;
                          &lt;%= art.author.full_name %&gt;
                      &lt;/td&gt;
                      &lt;td&gt;
                          &lt;%= art.category.name %&gt;
                      &lt;/td&gt;
                      &lt;td&gt;
                          &lt;%= art.updated_at.strftime("%d/%m/%Y %H:%M") %&gt;
                      &lt;/td&gt;
                      &lt;td&gt;
                          &lt;%= art.featured? ? "Yes" : "No" %&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;% end %&gt;
              &lt;/tbody&gt;
  </code></pre>
  
  <p><strong>blog articles</strong></p>
  
  <pre><code>            &lt;tbody&gt;
              &lt;% @barticles.each do |art| %&gt;
                  &lt;tr&gt;
                      &lt;td&gt;
                          &lt;%= art.title %&gt;
                      &lt;/td&gt;
                      &lt;td&gt;
                          &lt;%= art.author.full_name %&gt;
                      &lt;/td&gt;
                      &lt;td&gt;
                          &lt;%= art.category.name %&gt;
                      &lt;/td&gt;
                      &lt;td&gt;
                          &lt;%= art.updated_at.strftime("%d/%m/%Y %H:%M") %&gt;
                      &lt;/td&gt;
                      &lt;td&gt;
                          &lt;%= art.featured? ? "Yes" : "No" %&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;% end %&gt;
              &lt;/tbody&gt;
  </code></pre>
  
  <p><strong>admins</strong></p>
  
  <pre><code>            &lt;tbody&gt;
              &lt;% @admins.each do |admin| %&gt;
                  &lt;tr&gt;
                      &lt;td&gt;
                          &lt;%= admin.full_name %&gt;
                      &lt;/td&gt;
                      &lt;td&gt;
                          &lt;%= admin.email %&gt;
                      &lt;/td&gt;
                      &lt;td&gt;
                          &lt;%= !admin.twitter.nil? ? admin.twitter : "N/A" %&gt;
                      &lt;/td&gt;
                      &lt;td&gt;
                          &lt;%= !admin.facebook.nil? ? admin.facebook : "N/A" %&gt;
                      &lt;/td&gt;
                      &lt;td&gt;
                          &lt;%= !admin.phone.nil? ? admin.phone : "N/A" %&gt;
                      &lt;/td&gt;
                      &lt;td&gt;
                          &lt;%= admin.posts.count %&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;% end %&gt;
              &lt;/tbody&gt;
  </code></pre>
  
  <p><strong>bloggers</strong></p>
  
  <pre><code>                &lt;tbody&gt; 
                  &lt;% @bloggers.each do |blogger| %&gt;
                      &lt;tr&gt;
                          &lt;td&gt;
                              &lt;%= blogger.full_name %&gt;
                          &lt;/td&gt;
                          &lt;td&gt;
                              &lt;%= blogger.email %&gt;
                          &lt;/td&gt;
                          &lt;td&gt;
                              &lt;%= !blogger.twitter.nil? ? blogger.twitter : "N/A" %&gt;
                          &lt;/td&gt;
                          &lt;td&gt;
                              &lt;%= !blogger.facebook.nil? ? blogger.facebook : "N/A" %&gt;
                          &lt;/td&gt;
                          &lt;td&gt;
                              &lt;%= !blogger.phone.nil? ? blogger.phone : "N/A" %&gt;
                          &lt;/td&gt;
                          &lt;td&gt;
                              &lt;%= blogger.posts.count %&gt;
                          &lt;/td&gt;
                      &lt;/tr&gt;
                      &lt;% end %&gt;
                  &lt;/tbody&gt;
  </code></pre>