简体   繁体   中英

Creating Controllers in ROR for MYSQL tables

I have created a new MySQL DB app through ruby console giving below command

rails new -demo -d mysql

Then I created 1 table through MySQL editor. In Ruby Console I started rails server and its working fine (checked 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

rails console

Then I entered below command to create controller class which in turn returned 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.

I read couple of tutorials and it was referred that controller classes gets automatically created in 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. My Android app will be using this DB.

It will be really helpful if somebody can provide relevant Beginner tutorial or sample examples using ROR for server side coding with 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. 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 ):

This will create your CategoryController file with two actions, index, show . 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.

  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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM