My First Ruby App

Hi All,

Here is my roadmap for my first app in ruby: 1) installed vbox and have ubuntu 10.04 lucid as guest os 2) using apache, passenger , mysql and ruby on rails. Installed all of them separately. so this is the stack i'm using.

i'm originally well versed in oracle /ms sql db design and pgm. so, created my backend db using mysql workbench (first time using mysql workbench. It is isn't like erwin but does the job for mysql)

Created my model in mysql wbench and generated the sql script file for creating tables. this is done. Installed phpmyadmin and using the gui of it for mysql gui purposes. Imported the sql script via phpmyadmin and the database is completed with all its structure.

Now, I wanted to use this backened db and use it for ruby on rails web app. My web app is a simple web app which has only seven tables and relationships within it and it exists only in mysql db right now.

i created the structure using rails -d mywebapp mysql

executed ruby script/server

and the web app is up and running with the default page.

now, i have a table called category in mysql db and want to get the CRUD pages in ruby on rails web app.

I really hate to retype all my column names in the scaffold command.

so i want to skip migration in rails. so i ran the command script/generate scaffold category -- skip-migration

but this doesn't seem to have created any CRUD pages for the table category in mysql db. What should i do to see the CRUD pages for the table category ? By CRUD, i mean the create category, edit category, delete, etc.

I am avoiding the scaffold command because i hate to type all my column names in that command line. Can I avoid that ? Is there a way out? Please help.

- Thanks Radha

hey radha please explain what you need actually if you trying to making a rough apps write down the table field here migrattion tell to create or update ot delete table in to db from the rails app. if you make a table in db without a migration how would you connect it from rails app . any further help you need please explain with code

kannav rajeev wrote:

Thanks for helping.. here is my comments.

hey radha please explain what you need actually if you trying to making a rough apps write down the table field here migrattion tell to create or you

i am not sure of what you mean by "write down the table field here". do u mean in the scaffold command or write here? anyway, here is the table name, category. columns: category_id int parent_category_id int category_title varchar(50) category_short_desc varchar (100) category_long_desc varchar(255) dt_created datetime dt_modified datetime user_created varchar(45) user_modified varchar(45) status varchar(1)

routes.rb file this file is modified to specify my mysql db name and username and password for accessing the database.

Since, routes.rb is configured, can't i just specify the tabale name and environment and access the table ? Can't ruby on rails use this info to grab the column names from this table?

This table with these columns exist in my mysqldb if i use the scaffolding command with "skip-no-migration" then i will have to retype all these commands in scaffold command line.

There are two reasons for not using scaffold command: 1) Typing all column names in it, when all database config info is in the routes.rb file, ruby on rails could figure that out by connecting to it, via table name. 2) datatype specification. there is no way to specify datatypes. Probably, i may need to change it afer scaffolding.. which i am not sure of the steps with. Let me know , if u have any suggestions for this.

3) so, is there anyway, i could manually create these CRUD pages? If so , how? Is there a website/page that describes crating all these CRUD pages manually without using the command scaffold?

update ot delete table in to db from the rails app. if you make a table in db without a migration how would you connect it from rails app .

Using rotes.rb file, where in i have specified the environment, dbname, username and pwd for accessing the db. isn't that sufficient for ruby to access? Hypothertically, given the db environment as input in the code, output could be anything related to that db. in this case, input >> environment and table_name and output is the column names.(or any ) Is there a plugin for this purpose? If so, I could use it , to build these CRUD pages manually .

5) Do u normaly use scaffold and then modify the code for adjusting the datatypes? or do u write it from scratch without using scaffold?

any further help you need please explain with code

Thanks for helping me out as i have to create these CRUD pages and want to take the best route. My database contains nearly 25 tables and few columns it.

Thanks in advance.

I think there is a bit confusion in comunnication .As i understand i am writing the process here. ruby script/generate scaffold catogary then in yours applicaciotn db/migration you will find a migration Create catogaryAdd the field as you like or want .Something like that t.integer category_id int t.integer parent_category_id int then Do "rake db:migrate" Or Make saperate Controller model and views and run Ruby script/generate migration catogary or Ruby script/generate migration add_columns_to_catogary in yours applicaciotn db/migration you will find a migration Create catogary/or in add_columns_to_catogary Add the field as you like or want Something like that t.integer category_id int t.integer parent_category_id int then Do “rake db:migrateroutes.rb file this file is modified to specify my mysql db name and username and password for accessing the database.

Please explain me how this happen **mysql db name and username and password for accessing the database were discussed in database.yml not in routes.rb **

Hi Radha,

As per the details given by you, you want to run the application with out using the database migrations and Rake command, provided by the ROR.

You have your own data base structure in that you have the tables in that data base, in database.yml you are going to give those details, and from that you need to do the CURD operations .

Is this the thing that you are looking for ? Let me know If I am wrong on this, based on your answer I will suggest you the appropriate way to deal with in your application.

hi rajeev thanks for helping me out. in the last section, i meant database.yml and not routes.rb. (my bad! it was a slip of the tongue! )

I am in the prcess of re-reading your explanation. At the first cut, as soon as i see the word migration, i feel like,... "I don't want to do migration to my database. because, the columns exist in my database already. " Then why would i do migration?

I know i am missing something here. can't figure out what it is.

thanks a ton !

radha.

kannav rajeev wrote:

Do you have Data in that table .if not please Delete that table and run migrations. then run ruby script/server If you have data or you do not want to delete them put the new fields in table which you want to create and then You have to define other fields in in you MVC separately that brings a lot of pain for you

Hi Murali,

This is excatly what i want! (Looks like my trip to tirupati got answered)

Murali Tirupati wrote:

Hi Radha,

As per the details given by you, you want to run the application with out using the database migrations and Rake command, provided by the ROR.

Yes!

You have your own data base structure in that you have the tables in that data base, in database.yml you are going to give those details, and from that you need to do the CURD operations .

Yes!

Is this the thing that you are looking for ?

Yes!

Let me know If I am wrong

on this, based on your answer I will suggest you the appropriate way to deal with in your application.

Yes! Yes! Yes! This is exactly what I'm looking for and has been googling a lot for the same.

And came across one article which said it could be done without scaffolding. and another said that use "--skip-migration in the scaffold line" and another talked about using TableClass.columns.each something like that. src: Scaffolding for pre-existing database table in 2.0.1 - Rails - Ruby-Forum

I have just got a simple db and has been fiddling around this for the past week or so.

Thanks a ton!

Hi Radha,

My id is sumudhar@gmail.com, you can send me the desired requirements, I will guide you how can you achieve this.

Aside from the fact that you would have been better off letting Rails create your DB via migrations instead of creating it by hand -- you're making this much more difficult than it is.

Your previous example doesn't adhere to Rails conventions:

anyway, here is the table name, category. # should be named 'categories' columns: category_id int # should be 'id' parent_category_id int category_title varchar(50) category_short_desc varchar (100) category_long_desc varchar(255) dt_created datetime # should be 'created_at' dt_modified datetime # should be 'update_at' user_created varchar(45) user_modified varchar(45) status varchar(1)

Then just create your model class, e.g. in app/models/category.rb

class Category < ActiveRecord::Base end

:: and you're done.

It would probably help you to create a trivial app via scaffolding just to see examples of Rails-convention models, controllers, etc.

FWIW,

thanks hassan Sorry , i had not been clear. My intention is to make use of the legacy db(a db which already exists) and see how i could create the CRUD pages without migration. (in this case, please assume this db as legacy db as it exists right now)

And assume that it is an existing db ad let us find solutions for it without running migrations.

I have also configured a variable (pluralize table names = false ) in the environment file so that ruby doesn't pluralize and will accept singular table names and have also set_primary_key as category_id in the category model. More details for the above:   # this is a config line added by ubuman on june24,2010   ActiveRecord::Base.pluralize_table_names = false

and this is set in ..myapp/config/initializers/new_rails_defaults.rb (since it is ruby 1.8.7 it should be set in this file rather than environment.rb.. it seems so, as per some googled doc)

given this scenario, if this is an existing db, how could we create the CRUD pages for this without doing migration.

thanks radha

Hassan Schroeder wrote:

hi murali,

thanks again! I'll send u an email when i get home. (emails are blocked in this office) and probably this may help others too who are looking for the same prob/solution.

here is the situation: Bullet-1) Assume my db is already existing Bullet-2) Assume i have a table called category for which i have provided the structure above. Similar to this i have 25 other tables in the pre existing db. but we will use this category table for now. And i will use the similar approach for other tables, which requires CRUD pages to be built. category table structure: category_id int parent_category_id int category_title varchar(50) category_short_desc varchar (100) category_long_desc varchar(255) dt_created datetime dt_modified datetime user_created varchar(45) user_modified varchar(45) status varchar(1)

Bullet-3) I want to build the CRUD pages for it, without doing migration. The reason i don't want to do migration is , the table is already existing so i dont want to do migration. If so, how do i go about and create the cRUD pages without migration for this table category.

Bullet-4) Since the table is already existing, i don't prefer to delete . That's why i'm avoiding migration.

Bullet-5) I have configured the necessary variables to accept singular tables names and for the pk. (you may check my reply for hassan above regarding the config file and the changes i have made for rails to accept this convention). so, i would say the pluralize names =false has been taken care of. And the set_primary_key=category_id has been taken care of by setting this is in the model for category.

Now what are the steps to create the CRUD pages wihtout migration for this table , which is already existing.

thanks again radha

- thanks,

Murali Tirupati wrote:

There's an active_scaffold plugin that (I believe) replicates the old 1.x Rails dynamic scaffolding. I've never used it, but it might work.

Otherwise, you'll just have to write your views, tests, etc. yourself.

HTH,

Hassan Schroeder wrote:

given this scenario, if this is an existing db, how could we create the CRUD pages for this without doing migration.

There's an active_scaffold plugin that (I believe) replicates the old 1.x Rails dynamic scaffolding. I've never used it, but it might work.

Otherwise, you'll just have to write your views, tests, etc. yourself.

HTH, -- Hassan Schroeder ------------------------ hassan.schroeder@gmail.com twitter: @hassan

i'll check active scaffold plugin. as i googled, a few had specified that it isn't working as specified. i am yet to delve into it. will keep it posted as i delve into it.

apart from that.. today, the very first page in the CRUD pages started working. Created a "list" page to list all categories and it listed all. used the steps from:

in the similar approach , will try to create the other CRUD pages too.

here are the pages: ################### model ########################### ##################category.rb######################## ../app/models# more category.rb class Category < ActiveRecord::Base set_table_name 'category' set_primary_key "category_id"

def self.find_all_categories   find(:all) end end

RailsFan Radha wrote:

Hassan Schroeder wrote:

given this scenario, if this is an existing db, how could we create the CRUD pages for this without doing migration.

There's an active_scaffold plugin that (I believe) replicates the old 1.x Rails dynamic scaffolding. I've never used it, but it might work.

Otherwise, you'll just have to write your views, tests, etc. yourself.

HTH, -- Hassan Schroeder ------------------------ hassan.schroeder@gmail.com twitter: @hassan

i'll check active scaffold plugin. as i googled, a few had specified that it isn't working as specified. i am yet to delve into it. will keep it posted as i delve into it.

apart from that.. today, the very first page in the CRUD pages started working. Created a "list" page to list all categories and it listed all. used the steps from: Ruby on Rails - Views

in the similar approach , will try to create the other CRUD pages too.

here are the pages: ################### model ########################### ##################category.rb######################## ../app/models# more category.rb class Category < ActiveRecord::Base set_table_name 'category' set_primary_key "category_id"

def self.find_all_categories   find(:all) end end #####################################################

##################controller##################### ##############category_controller.rb############# app/controllers# more category_controller.rb class CategoryController < ApplicationController     def index   # "List all categories"   list   render:action=>'list'     end    #--- List--    def list    @categories=Category.find_all_categories   end

end ##################################################

###################### views###################### ######################list.erb#################### ../app/views/category# more list.erb

<h1>list template for category <h1>

<% if @categories.blank? %> <p>There are not any categories currently in the system.</p> <% else %> <p>These are the current categories in our system</p> <ul id="categories"> <% @categories.each do |c| %> <li><%= link_to c.category_title, {:action => 'show', :id => c.category_id} -%>< /li> <% end %> </ul> <% end %>

####################################################

Summary: -------- For one controller and one action, these three files were created/modified: Currenlty, these three flles does only one action, which is "listing" all categories from the "category table". I think atleast one part of the CRUD is done. can we say so? I guess so. Files modified: -------------- model/category.rb controller/category_controller.rb views/list.erb

the next step is to prep for the "edit" action for category.

please let me know if i'm missing any.

thanks again, radha.

Update to this post:

"show" action for this category has been done. I think this is one among the CRUD pages. (create, read, update, delete) I guess, show => read

The following pages are created/modified for this "show" action for "category" controller:

RailsFan Radha wrote:

RailsFan Radha wrote:

RailsFan Radha wrote:

RailsFan Radha wrote:

Hassan Schroeder wrote:

given this scenario, if this is an existing db, how could we create the CRUD pages for this without doing migration.

There's an active_scaffold plugin that (I believe) replicates the old 1.x Rails dynamic scaffolding. I've never used it, but it might work.

Otherwise, you'll just have to write your views, tests, etc. yourself.

HTH, -- Hassan Schroeder ------------------------ hassan.schroeder@gmail.com twitter: @hassan

i'll check active scaffold plugin. as i googled, a few had specified that it isn't working as specified. i am yet to delve into it. will keep it posted as i delve into it.

apart from that.. today, the very first page in the CRUD pages started working. Created a "list" page to list all categories and it listed all. used the steps from: Ruby on Rails - Views

in the similar approach , will try to create the other CRUD pages too.

here are the pages: ################### model ########################### ##################category.rb######################## ../app/models# more category.rb class Category < ActiveRecord::Base set_table_name 'category' set_primary_key "category_id"

def self.find_all_categories   find(:all) end end #####################################################

##################controller##################### ##############category_controller.rb############# app/controllers# more category_controller.rb class CategoryController < ApplicationController     def index   # "List all categories"   list   render:action=>'list'     end    #--- List--    def list    @categories=Category.find_all_categories   end

end ##################################################

###################### views###################### ######################list.erb#################### ../app/views/category# more list.erb

<h1>list template for category <h1>

<% if @categories.blank? %> <p>There are not any categories currently in the system.</p> <% else %> <p>These are the current categories in our system</p> <ul id="categories"> <% @categories.each do |c| %> <li><%= link_to c.category_title, {:action => 'show', :id => c.category_id} -%>< /li> <% end %> </ul> <% end %>

####################################################

Summary: -------- For one controller and one action, these three files were created/modified: Currenlty, these three flles does only one action, which is "listing" all categories from the "category table". I think atleast one part of the CRUD is done. can we say so? I guess so. Files modified: -------------- model/category.rb controller/category_controller.rb views/list.erb

the next step is to prep for the "edit" action for category.

please let me know if i'm missing any.

thanks again, radha.

Update to this post:

"show" action for this category has been done. I think this is one among the CRUD pages. (create, read, update, delete) I guess, show => read

The following pages are created/modified for this "show" action for "category" controller: ------------------------------------------------------------------------------ app/controller/category_controller.rb ( added show def to this file) app/views/category/show.erb (new template created. this is a new file)

################## app/controller/category_controller.rb ###########   def show      @category=Category.find(params[:category_id])   end

#######################################################################

################### view ########################################## /app/views/category# more show.erb <h1><%= @category.category_title %></h1> <p><strong>Title: </strong> $<%= @category.category_title %><br /> <strong>Short_Description :</strong> <%= @category.category_short_desc %><br /> <strong>Long_Description :</strong> <%= @category.category_long_desc %><br /> <strong>Status :</strong> <%= @category.status%><br />

<strong>Created Date:</strong> <%= @category.dt_created %><br /> <strong>Modified Date:</strong> <%= @category.dt_modified %><br /> <strong>Created User:</strong> <%= @category.user_created %><br /> <strong>Modified User:</strong> <%= @category.user_modified %><br /> </p> <p><%= @category.category_title %></p> <hr /> <%= link_to 'Back', {:action => 'list'} %> ################.#################################

Summary: ----------------- To create the "show" action, controller file was modified and a new view template was created. Model remains inchanged.

So, this works! The link created in the abover list.erb, shows this show.erb page when clicked. The link works and that means the "show" action is working.

------------------------ Please let me know if I'm missing any in this route.

Just thought of updating this here, as this inspires me to move forward, thinking i have people here to discuss with. that makes all the big difference.

I will keep you all posted.

- thanks again, radha.

#####################################################################

Second Update to this Post:

"Insert/Add" action , one among the CRUD pages is working:

Here are the changes made for this action:

Step-1: -------- app/controller/category_controller.rb ---------------------------------------- added this def create part to it. -------------- def create       @category = Category.new(params[:category])       if @category.save             redirect_to :action => 'list'       else         # @subjects = Subject.find(:all)            render :action => 'new'       end    end

Step-2: ------- app/views/category/new.erb new file added. ---------- here is the content of this file

<h1>Add new Category </h1> <%= form_tag :action => 'create'%> <p><label for="category_title">Category Title</label>: <%= text_field 'category', 'category_title' %></p>

<p><label for="categoy_short_desc">Category Short Description</label>: <%= text_field 'category', 'category_short_desc' %></p>

<p><label for="categoy_long_desc">Category Long Description</label>: <%= text_area 'category', 'category_long_desc' %></p>

<%= submit_tag "Create" %> <%= form_tag %> <%= link_to 'Back', {:action => 'list'} %>

------------------------------------------------------

Summary: -- "Add" action , which is one among the CRUD pages is created. (though no validations have been taken care of. it should be in the later steps) -- new file new.erb has been created for this action -- category_controller.rb has been modified for this action

Tested this by adding a new category. It adds a new category and then takes the user to the list page. well, haven't done any tesing using fixtures/rails.. not sure of how that works.. so haven't touched that part. and not sure what difference that would make either.

Please let me know if i'm missing any.

thanks, radha

thanks, radha

################################################################

third update to this post:

Edit action, one among the CRUD pages is created:

Files added/modifield: 1) app/controller/category_controller.rb modified 2) app/views/category/edit.erb added newly

content shown below

"I should have removed the quoted text in the previous posts too. Just realized it."