Sinatra:"/route"处的NameError,未定义的本地变量或方法

问题描述:

我正在尝试为cms网站构建管理员后端.

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

我已经在 routes.rb

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

视图(categories.erb)文件的内容.

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>

当我浏览到/categories路由时,服务器会显示此错误

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.

问题可能不在于应用程序结构(app.rb中的require序列),因为在get '/categories'之前,我已经定义了post '/login'路由,该路由将对照数据库记录检查用户输入.它可以按我的意愿工作.

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

更新

我用相同的方法列出了其他表/模型,它们都按我的预期工作,但是分类.

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

和相应的视图文件 网站文章

            <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>

博客文章

            <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>

管理员

            <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>

博客

                <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>

您的源代码中有些奇怪的字符.这不是<%= с.posts.count %>行中的普通"拉丁语c,它是 U + 0441,西里尔小写字母ES .看起来就像是拉丁文c.

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.

要解决此问题,只需删除字符,然后使用普通的c对其进行重写.

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