[英]Creating Controllers in ROR for MYSQL tables
I have created a new MySQL DB app through ruby console giving below command 我已经通过ruby控制台创建了一个新的MySQL DB应用,并给出以下命令
rails new -demo -d mysql
Then I created 1 table through MySQL editor. 然后我通过MySQL编辑器创建了1个表。 In Ruby Console I started rails server and its working fine (checked localhost:3000). 在Ruby Console中,我启动了rails服务器并正常工作(检查localhost:3000)。 Now I am unable to create controller class for this table. 现在,我无法为该表创建控制器类。 I tried many commands given on net but nothing seems to be working. 我尝试了网上给出的许多命令,但似乎无济于事。
From Ruby command console I switched to rails console giving below command 我从Ruby命令控制台切换到了提供以下命令的Rails控制台
rails console
Then I entered below command to create controller class which in turn returned nil 然后我输入以下命令来创建控制器类,该类又返回nil
irb(main):003:0> class CategoryController < ApplicationController
irb(main):004:1> def index
irb(main):005:2> end
irb(main):006:1> def show
irb(main):007:2> end
irb(main):008:1> end
=> nil
But no such class got created in app/controller folder of my application. 但是在我的应用程序的app / controller文件夹中没有创建此类。
I read couple of tutorials and it was referred that controller classes gets automatically created in rails. 我读了几本教程,据称控制器类是在Rails中自动创建的。 I tried those too but they didn't run. 我也尝试过这些,但它们没有运行。
I am not sure if I am missing something. 我不确定是否遗漏了一些东西。 Could someone please help to guide further steps. 有人可以帮助指导进一步的步骤。 I am using ROR only on server side. 我仅在服务器端使用ROR。 My Android app will be using this DB. 我的Android应用将使用此数据库。
It will be really helpful if somebody can provide relevant Beginner tutorial or sample examples using ROR for server side coding with MySQL. 如果有人可以提供相关的初学者教程或使用ROR进行MySQL服务器端编码的示例示例,将非常有帮助。
Thanks. 谢谢。
The Rails console is used to test code at runtime, not to generate code which is actually stored in files to be used again. Rails控制台用于在运行时测试代码,而不是生成实际上存储在文件中以供再次使用的代码。 The correct way to generate a controller is via the rails generate controller
command from your system's command line (not from the Rails console or irb
): 生成控制器的正确方法是通过rails generate controller
命令从系统的命令行(而不是从Rails控制台或irb
):
This will create your CategoryController
file with two actions, index, show
. 这将使用两个操作index, show
创建您的CategoryController
文件。 You may omit those actions or add additional actions. 您可以省略这些操作或添加其他操作。
rails generate controller CategoryController index show
This will result in output similar to what's below, assuming all your gem dependencies are correctly met. 假设正确地满足了您所有的gem依赖关系,这将导致输出类似于以下内容。
create app/controllers/category_controller_controller.rb
route get "category_controller/show"
route get "category_controller/index"
invoke erb
create app/views/category_controller
create app/views/category_controller/index.html.erb
create app/views/category_controller/show.html.erb
invoke test_unit
create test/controllers/category_controller_controller_test.rb
invoke helper
create app/helpers/category_controller_helper.rb
invoke test_unit
create test/helpers/category_controller_helper_test.rb
invoke assets
invoke js
create app/assets/javascripts/category_controller.js
invoke scss
create app/assets/stylesheets/category_controller.css.scss
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.