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

On Fri, Jun 25, 2010 at 8:56 AM, Free B. [email protected] wrote:

Radha
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.


Thanks:
Rajeev sharma

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 .

  1. 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.

On Fri, Jun 25, 2010 at 8:56 AM, Free B. [email protected] wrote:

Radha
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.


Thanks:
Rajeev sharma

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:migrate

routes.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

On Fri, Jun 25, 2010 at 6:06 PM, RailsFan R.
[email protected]wrote:

do u mean in the scaffold command or write here?
user_modified varchar(45)

  1. datatype specification. there is no way to specify datatypes.

Is there a plugin for this purpose?

any further help you need please explain with code
On Fri, Jun 25, 2010 at 8:56 AM, Free B. [email protected] wrote:

“Ruby on Rails: Talk” group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected][email protected]
.
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.


Thanks:
Rajeev sharma

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

On Fri, Jun 25, 2010 at 11:08 PM, RailsFan R.
[email protected]wrote:

Then why would i do migration?

then in yours applicaciotn db/migration you will find a migration Create

in

[email protected][email protected]
Thanks:
[email protected][email protected]
.
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.


Thanks:
Rajeev sharma

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:

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:migrate

routes.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

On Fri, Jun 25, 2010 at 6:06 PM, RailsFan R.
[email protected]wrote:

do u mean in the scaffold command or write here?
user_modified varchar(45)

  1. datatype specification. there is no way to specify datatypes.

Is there a plugin for this purpose?

any further help you need please explain with code
On Fri, Jun 25, 2010 at 8:56 AM, Free B. [email protected] wrote:

“Ruby on Rails: Talk” group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected][email protected]
.
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.


Thanks:
Rajeev sharma

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 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 Rajeev,
Thanks for helping!

i really hate to delete the table.
I would like to make use of the table and point ror to grab what ever it
wants by using the config files and commands, without retyping the
fields.
is there something called tableClass.columns which i could use for thsi
purpose?
If so, where and how?
i didn’t get the full details, but found this hint in (look at the
bottom of this page for this hint)
http://www.ruby-forum.com/topic/134594

thanks,
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

On Fri, Jun 25, 2010 at 11:08 PM, RailsFan R.
[email protected]wrote:

Then why would i do migration?

then in yours applicaciotn db/migration you will find a migration Create

in

[email protected][email protected]
Thanks:
[email protected][email protected]
.
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.


Thanks:
Rajeev sharma

Hi Radha,

My id is [email protected], you can send me the desired requirements, I
will guide you how can you achieve this.

On Sat, Jun 26, 2010 at 11:05 PM, RailsFan R.
[email protected]wrote:

As per the details given by you, you want to run the application with

Thanks a ton!
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.


Thanks & Regards,
MuraliDharaRao.T
+91-9642234646

On Sat, Jun 26, 2010 at 10:35 AM, RailsFan R. [email protected]
wrote:

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

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,

Hassan S. ------------------------ [email protected]
twitter: @hassan

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 S. wrote:

On Sat, Jun 26, 2010 at 10:35 AM, RailsFan R. [email protected]
wrote:

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

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,

Hassan S. ------------------------ [email protected]
twitter: @hassan

On Sat, Jun 26, 2010 at 11:56 AM, RailsFan R. [email protected]
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 S. ------------------------ [email protected]
twitter: @hassan

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:

Hi Radha,

My id is [email protected], you can send me the desired requirements, I
will guide you how can you achieve this.

On Sat, Jun 26, 2010 at 11:05 PM, RailsFan R.
[email protected]wrote:

As per the details given by you, you want to run the application with

Thanks a ton!
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.


Thanks & Regards,
MuraliDharaRao.T
+91-9642234646

Hassan S. wrote:

On Sat, Jun 26, 2010 at 11:56 AM, RailsFan R. [email protected]
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 S. ------------------------ [email protected]
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
#####################################################

##################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

list template for category

<% if @categories.blank? %>

There are not any categories currently in the system.

<% else %>

These are the current categories in our system

    <% @categories.each do |c| %>
  • <%= link_to c.category_title, {:action => 'show', :id => c.category_id} -%>< /li> <% end %>
<% 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.

RailsFan R. wrote:

Hassan S. wrote:

On Sat, Jun 26, 2010 at 11:56 AM, RailsFan R. [email protected]
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 S. ------------------------ [email protected]
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

list template for category

<% if @categories.blank? %>

There are not any categories currently in the system.

<% else %>

These are the current categories in our system

    <% @categories.each do |c| %>
  • <%= link_to c.category_title, {:action => 'show', :id => c.category_id} -%>< /li> <% end %>
<% 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

<%= @category.category_title %>

Title: $<%= @category.category_title %>
Short_Description : <%= @category.category_short_desc %>
Long_Description : <%= @category.category_long_desc %>
Status : <%= @category.status%>

Created Date: <%= @category.dt_created %>

Modified Date: <%= @category.dt_modified %>

Created User: <%= @category.user_created %>

Modified User: <%= @category.user_modified %>

<%= @category.category_title %>


<%= 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.

RailsFan R. wrote:

RailsFan R. wrote:

Hassan S. wrote:

On Sat, Jun 26, 2010 at 11:56 AM, RailsFan R. [email protected]
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 S. ------------------------ [email protected]
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

list template for category

<% if @categories.blank? %>

There are not any categories currently in the system.

<% else %>

These are the current categories in our system

    <% @categories.each do |c| %>
  • <%= link_to c.category_title, {:action => 'show', :id => c.category_id} -%>< /li> <% end %>
<% 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

<%= @category.category_title %>

Title: $<%= @category.category_title %>
Short_Description : <%= @category.category_short_desc %>
Long_Description : <%= @category.category_long_desc %>
Status : <%= @category.status%>

Created Date: <%= @category.dt_created %>

Modified Date: <%= @category.dt_modified %>

Created User: <%= @category.user_created %>

Modified User: <%= @category.user_modified %>

<%= @category.category_title %>


<%= 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

Add new Category

<%= form_tag :action => 'create'%>

Category Title: <%= text_field 'category', 'category_title' %>

Category Short Description: <%= text_field 'category', 'category_short_desc' %>

Category Long Description: <%= text_area 'category', 'category_long_desc' %>

<%= 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

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

RailsFan R. wrote:

RailsFan R. wrote:

RailsFan R. wrote:

Hassan S. wrote:

On Sat, Jun 26, 2010 at 11:56 AM, RailsFan R. [email protected]
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 S. ------------------------ [email protected]
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

list template for category

<% if @categories.blank? %>

There are not any categories currently in the system.

<% else %>

These are the current categories in our system

    <% @categories.each do |c| %>
  • <%= link_to c.category_title, {:action => 'show', :id => c.category_id} -%>< /li> <% end %>
<% 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

<%= @category.category_title %>

Title: $<%= @category.category_title %>
Short_Description : <%= @category.category_short_desc %>
Long_Description : <%= @category.category_long_desc %>
Status : <%= @category.status%>

Created Date: <%= @category.dt_created %>

Modified Date: <%= @category.dt_modified %>

Created User: <%= @category.user_created %>

Modified User: <%= @category.user_modified %>

<%= @category.category_title %>


<%= 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

Add new Category

<%= form_tag :action => 'create'%>

Category Title: <%= text_field 'category', 'category_title' %>

Category Short Description: <%= text_field 'category', 'category_short_desc' %>

Category Long Description: <%= text_area 'category', 'category_long_desc' %>

<%= 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

added this part to app/controller/category_controller.rb
#-------------edit -----------
def edit
@category=Category.find(params[:id])
# @book = Book.find(params[:id])
# @subjects = Subject.find(:all)
end

#######################################
2) edit.erb content

/app/views/category# more ed*

Edit new Category

<%= form_tag :action => 'update', :category_id => @category %>

Category Title: <%= text_field 'category', 'category_title' %>

Category Short Description: <%= text_field 'category', 'category_short_desc' %>

Category Long Description: <%= text_area 'category', 'category_long_desc' %>

<%= submit_tag “Update” %>
<%= form_tag %>
<%= link_to ‘Back’, {:action => ‘list’} %>

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

Summary:

edit.erb is very similar to new.erb which adds a new category.
The only difference is, change the action to “update and define an id”.
Change the button label to update.

Edit works . Validation is yet to be incorporated. may be later.
So far, List, Show, AddNew, Edit actions have been taken care of.
In other words,
Create, Read and Edit , which are part of the CRUD pages have been
created.

the other one which is left out is delete. we will see that next.

Please let me know if I am missing any.

thanks again.
radha

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


I posted this last night. somehow , i don’t see it here.
so resposting this fourth update, which is purely for “delete” action
and this completed CRUD pages setup.

Fourth update to this post:

Delete action, one among the CRUD actions implemented.

File modified/added for this DElete action implementation:

  1. controller file modified to add this action
    app/controller/category/category_controller.rb modified
    add this part to it.
    #-----------Delete ------------
    def delete
    Category.find(params[:id]).destroy
    redirect_to :action => ‘list’
    end

  2. list.erb modified to create the link
    <%= link_to “Delete”, {:action => ‘delete’, :id => c.category_id},
    :confirm
    => “Are you sure you want to delete this item?” %>

So far, Create, Read, Edit and Delete actions which are part of CRUD
have been touched at the basic level.

Please let me know if I’m missing any here.

Next steps:
Probably addition validation to fields.
How to add the test fixture here? i have no idea.
And then creating layouts for these pages. at the least a simple
navigator to the right, a top navigator and the right content page and
the bottom footer page.
hopefully these are the next steps…
if u have any suggestions / sites for the above steps please post it.

##################################################################3

thanks again.
radha