ActionController :: RoutingError(未初始化的常量AdminsController)

时间:2011-11-17 06:34:22

标签: ruby-on-rails

我正在使用带有dhtmlxGrid的Rails 3。我创建了一个名为admin的控制器,我们定义了3个函数,例如viewdatadbaction及其视图文件。

下面是我的admin控制器文件。

 class AdminController < ApplicationController

 def view

 end

 def data

 @users = User.all()

 end

def dbaction
    #called for all db actions

    first_name = params["c0"]
    last_name    = params["c1"]
    phone            = params["c2"]

    @mode = params["!nativeeditor_status"]

    @id = params["gr_id"]
    case @mode
        when "inserted"
            user = User.new
            user.first_name = first_name
            user.last_name = last_name
            user.phone = phone
            user.save!

            @tid = user.id
        when "deleted"
            user=User.find(@id)
            user.destroy

            @tid = @id
        when "updated"
            user=User.find(@id)
            user.first_name = first_name
            user.last_name = last_name
            user.phone = phone
            user.save!

            @tid = @id
    end 
 end 
end

以下是我的view.rhtml文件。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <script src="/javascripts/codebase/dhtmlxcommon.js" type="text/javascript"       charset="utf-8"></script>
    <script src="/javascripts/codebase/dhtmlxgrid.js" type="text/javascript" charset="utf-8"></script>
    <script src="/javascripts/codebase/dhtmlxgridcell.js" type="text/javascript" charset="utf-8"></script>

    <link rel="stylesheet" href="/javascripts/codebase/dhtmlxgrid.css" type="text/css" media="screen" charset="utf-8">
    <link rel="stylesheet" href="/javascripts/codebase/skins/dhtmlxgrid_dhx_skyblue.css" type="text/css" media="screen" charset="utf-8">
    <script src="/javascripts/codebase/dhtmlxdataprocessor.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
    <div id="grid_here" style="width:600px; height:400px;">
    </div>
    <script type="text/javascript" charset="utf-8">
        var grid = new dhtmlXGridObject("grid_here");
        grid.setImagePath("/javascripts/codebase/imgs/");
        grid.setHeader("First name, Last name, Phone");
        grid.setInitWidths("100,100,*");
        grid.setSkin("dhx_skyblue");
        grid.init();
        grid.load("/admin/data.xml");

        db = new dataProcessor("/admin/dbaction.xml");

        db.init(grid);
    </script>


   <input type="button" value="Add" onclick="grid.addRow(grid.uid(),'new user')">
    <input type="button" value="Delete" onclick="grid.deleteSelectedRows()">
</body>

下面是我的data.rxml文件。

xml.instruct! :xml, :version=>"1.0" 

xml.tag!("rows") do
  @users.each do |user|
    xml.tag!("row",{ "id" => user.id }) do
        xml.tag!("cell", user.first_name)
        xml.tag!("cell", user.last_name)
        xml.tag!("cell", user.phone)
     end
  end
end
下面的

是我的dbaction.rxml文件。

xml.instruct! :xml, :version=>"1.0" 

 xml.tag!("data") do

 xml.tag!("action",{ "type" => @mode, "sid" => @id, "tid" => @tid }) 

end

下面是我的route.rb文件。

resource :admin do
  collection do
     get 'view'
     get 'data'
     get 'dbaction'
   end
 end

以下是迁移文件。

class CreateUsers < ActiveRecord::Migration
   def self.up

    create_table :users do |t|
        t.string :first_name
        t.string :last_name
        t.string :phone
    t.timestamps
 end

 user.create(
    :first_name => "John",
    :last_name => "Smith",
    :phone => "997347854")

user.create(
    :first_name => "Stanislav",
    :last_name => "Wolski",
    :phone => "465764778"
  end

 def self.down
  drop_table :users
 end

这是我用来开发上述程序的链接 http://www.dhtmlx.com/blog/?p=426

2 个答案:

答案 0 :(得分:1)

尝试将控制器名称更改为AdminsController。 Rails似乎更喜欢多个控制器名称。

答案 1 :(得分:0)

只是添加@Paul所说的内容。当你为你的控制器使用复数时,Rails真的很喜欢它,但是如果你可以匹配文件名 admin_controller.rb 并且类名 AdminController 那么这也应该可行。

否则,配置方法上的rails约定规定了控制器名称的复数。