rubysinatranameerrorruby-datamapper

Sinatra: NameError at '/route', undefined local variable or method


I'm trying to build an admin backend for a cms website.

this is the structure of my application

├── app.rb
├── Gemfile
├── models
│   └── models.rb
├── routes
│   └── routes.rb
└── views
    ├── categories.erb
    ├── # ... other view files

app.rb

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

models.rb

class Category
  include DataMapper::Resource

  property :id,         Serial
  property :name,       String

  has n, :posts
end
# other model definitons

I've defined a categories route inside my routes.rb

...
get '/categories' do 
    @categories = Category.all
    erb :categories
end 
...

Contents of view (categories.erb) file.

                #table headers   
                <tbody>
                <% @categories.each do |c| %>
                    <tr>
                        <td>
                            <%= c.id %>
                        </td>
                        <td>
                            <%= c.name %>
                        </td>
                        <td>
                            <%= с.posts.count %>
                        </td>
                        <td>
                            <%= c.posts(:order => [:updated_at.desc]).first.updated_at.strftime("%d/%m/%Y") %>
                        </td>
                    </tr>
                <% end %>
                </tbody>

When I browse to /categories route, server spits this error

NameError at /categories
undefined local variable or method `с' for #<Sinatra::Application:0x0000000284bc08>

I have not faced such problem before. And really don't know what is going on.

The problem probably is not with application structure (the require sequences inside app.rb) because, before get '/categories' I've defined a post '/login' route which checks user inputs against database records. And it works as I want.

post '/login' do 
    email = params[:email]
    pwd = params[:password]

    user = Author.first(:email=>email)

    if user.nil?
        redirect to ('/register')
    elsif !user.nil?
        if Digest::MD5.hexdigest(pwd) == user.password
        ... #and so on

UPDATE

I'm listing other table/models with same methods, and all of them works as i expected, but categories.

other routes  
get '/articles' do 
    @articles = Post.all(:is_blog_post => false)
    erb :site_articles
end
##blog articles
get '/blogposts' do 
    @barticles = Post.all(:is_blog_post => true)
    erb :blog_articles
end
#users
get '/admin_users' do
    @admins = Author.all(:is_admin=>true)
    erb :admin_users 
end

#bloggers
get '/bloggers' do
    @bloggers = Author.all(:is_admin=>false)
    erb :blog_users 
end

and corresponding view files site articles

            <tbody>
            <% @articles.each do |art| %>
                <tr>
                    <td>
                        <%= art.title %>
                    </td>
                    <td>
                        <%= art.author.full_name %>
                    </td>
                    <td>
                        <%= art.category.name %>
                    </td>
                    <td>
                        <%= art.updated_at.strftime("%d/%m/%Y %H:%M") %>
                    </td>
                    <td>
                        <%= art.featured? ? "Yes" : "No" %>
                    </td>
                </tr>
                <% end %>
            </tbody>

blog articles

            <tbody>
            <% @barticles.each do |art| %>
                <tr>
                    <td>
                        <%= art.title %>
                    </td>
                    <td>
                        <%= art.author.full_name %>
                    </td>
                    <td>
                        <%= art.category.name %>
                    </td>
                    <td>
                        <%= art.updated_at.strftime("%d/%m/%Y %H:%M") %>
                    </td>
                    <td>
                        <%= art.featured? ? "Yes" : "No" %>
                    </td>
                </tr>
                <% end %>
            </tbody>

admins

            <tbody>
            <% @admins.each do |admin| %>
                <tr>
                    <td>
                        <%= admin.full_name %>
                    </td>
                    <td>
                        <%= admin.email %>
                    </td>
                    <td>
                        <%= !admin.twitter.nil? ? admin.twitter : "N/A" %>
                    </td>
                    <td>
                        <%= !admin.facebook.nil? ? admin.facebook : "N/A" %>
                    </td>
                    <td>
                        <%= !admin.phone.nil? ? admin.phone : "N/A" %>
                    </td>
                    <td>
                        <%= admin.posts.count %>
                    </td>
                </tr>
                <% end %>
            </tbody>

bloggers

                <tbody> 
                <% @bloggers.each do |blogger| %>
                    <tr>
                        <td>
                            <%= blogger.full_name %>
                        </td>
                        <td>
                            <%= blogger.email %>
                        </td>
                        <td>
                            <%= !blogger.twitter.nil? ? blogger.twitter : "N/A" %>
                        </td>
                        <td>
                            <%= !blogger.facebook.nil? ? blogger.facebook : "N/A" %>
                        </td>
                        <td>
                            <%= !blogger.phone.nil? ? blogger.phone : "N/A" %>
                        </td>
                        <td>
                            <%= blogger.posts.count %>
                        </td>
                    </tr>
                    <% end %>
                </tbody>

Solution

  • You’ve somehow got an odd character in your source. That’s not a “normal” latin c in the line <%= с.posts.count %>, it’s U+0441, CYRILLIC SMALL LETTER ES. It just looks like a latin c.

    To fix it just delete the character and re-write it with a normal c.