Creating Controllers in ROR for MYSQL tables - mysql

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

Related

I am not being able to use yii console commands in yii 2.0.16 version

I have started using yii 2.0.16 version. When I made migration command in order to create initial user table, it didn't work. Instead, it showed view like this:
Can you help, please

Using CakePHP 3.0 plugin

I'm currently building a new CakePHP app with version 3.0.0-RC1, and trying to install and use the jasig/phpCAS plugin. Using this guide, I've run the following command from the command prompt: composer require jasig/phpcas
This correctly copies the jasig/phpcas files into the vendor directory of my app, but one of the other files that the guide says should be updated (vendor/cakephp-plugins.php) doesn't even exist.
I've had a tough time accessing the plugin. I want to be able to call its static methods, and I keep getting errors of the form: Error: Class 'App\Controller\phpCAS' not found. (The exact directory in the error changes depending on where I'm calling the method from.)
I don't know if this is due to not having the cakephp-plugins.php file, or if I'm not calling the plugin correctly. It's my understanding that if the plugin is loaded I should just be able to call static methods on it like this: phpCAS::methodName()
First of all jasig/phpcas is not a CakePHP plugin. And the vendor/cakephp-plugins.php file is created by the CakePHP plugin installer, so if you don't see such a file, you seem to have either not installed any plugins yet, or you are not using a recent version of the installer, as the creation of this file has been introduced just recently.
Regarding the error about the class not being found, you are missing the leading namespace separator (\phpCAS::methodName()) to access the class in the global namespace, respectively you are missing a proper import (use phpCAS;) that would make the class available in the current namespace.
In case you are not familiar with namespaces, you may want to start with: http://php.net/namespaces

Django database watchdog save signal outside django

I have the following problem:
I Am using a Django framework.
One of the parts in a system (non-django) writes to the database, in the same database that django is using.
I want to have a signal when an object is being saved. It's a django model object but not saved via django, but directly in the mysql database.
Is there a way django can watch save-actions in his database when it's not being saved by django?
The neatest way would be: create an Api, and let the save action run through this api. The save signal can than be django default. (but this depends on some work of externals... so not the prefered route... for future development it sure is).
Another option is to implement celery and create a task that frequently looks whether one of the saved objects has had no follow up..... (also quit some puzzling I guess to get this up and running)
But there might be an easier... for me unknown?
I saw django watchdog solutions for file systems... not for databases (probably because django has this build in... when properly done through django)
to complex it: I test and develop locally with sqlite .... but the save signal I can put in my tests without needing to get this locally working.... as long as it works in mysql, I Am happy.
You can try this solution:
Create a new table 'django_watch' with one column 'object_id' (add other columns like 'created_datetime' etc according to your standards);
Lets say your main table is 'object'. Add a mysql trigger for the INSERT event on this table.
You should add an extra insert query inside the trigger to insert the object_id into 'django_watch' table.
Now you can have a cronjob that will be inpecting the new table 'django_watch' (for updations in Django objects) and perform necessary actions. You can run this cronjob continuously with some 1 minute delay (upto you).
In the end, I wrote an api that can be called by the thirdparty module. I delivered the code to logon on django using c-code to this api and call the GET of this api. (using django rest framework). This api just saves the object (the id given in the url), and from there on it's default django. The only thing the third party had to do is build in my code to call the api as well....
Maybe not the best solution, but the best to implement for my problem....

Starting SQLAlchemy session from separate file

I created my models in a file and can successfully add and alter objects which successfully commit to the database when run in a function beneath the model declaration. When I import my Session and model classes into a separate script, I can successfully run a query, but if I update the object and run an update the db does not get updated. The same code works well when run beneath where I defined my models and I'm totally confused why this is. Am I confused as to how I should work with and update my database?
haha, I must have been looking at it too closely for too long. In the snippet of code that wasn't working I was calling session.commit, not session.commit().

RailsTutorial - Testing - What to put for Database.YML for MySQL?

First, thanks for your help.
As a noob, I've been happily chugging along this well-known tutorial:
http://ruby.railstutorial.org/chapters/static-pages#top
and I'm caught in a section where I enter
rspec spec/
where I receive 2 errors. The 2 errors are in the format of
PagesController GET 'home' should be
successful
Failure/Error: Unable to find matching line from backtrace
Mysql2::Error:Unknown database 'xyz.rb'
where "home" is the name of the action/page. The xyz.rb is the arbitrary
database name I have listed under the test section in database.yml. I
have already raked the database.
I'm pretty sure my problem has to do with how I'm not going with the
sqlite3 in the tutorial but instead with mysql2. The resources I have
managed to find only give guidance on what to input for the development
section in database.yml, but not for the rest of the sections like
"test". So, my question is, what exactly does this error mean, how to
fix it, and how should I configure my database.yml file? I tried
entering a file I see in my db folder like schema.rb, but this renders
the same error.
Thank you very much for your help.
You have two options:
Set up another sqlite database, as is default with a new rails application, or
Create another MySQL database, the same way you set up your development database, with a different name (such as test) and use that for testing.
Here's my database.yml file from the Rails Tutorial that uses SQLite for all the databases; you should be able to copy the test section if you decide to go with #1 above.