Hi,
I am trying to learn Ruby on Rails programming. I have installed
“Instant Rails” and trying to build a small application. I created a
table called “types” for the application.
Below is the structure of the types database table.
±---------------±----------------±-----±----±--------±---------------+
| Field | Type | Null | Key | Default | Extra |
±---------------±----------------±-----±----±--------±---------------+
| item_type_id | int(4) unsigned | NO | PRI | NULL | auto_increment |
| item_type | varchar(3) | YES | | NULL | |
| item_type_desc | varchar(50) | YES | | NULL | |
| created_by | varchar(30) | YES | | NULL | |
| created_on | date | YES | | NULL | |
| modified_by | varchar(30) | YES | | NULL | |
| modified_date | date | YES | | NULL | |
±---------------±----------------±-----±----±--------±---------------+
I tried to build a rails application using the articles “Rolling with
Ruby on Rails” and “Rolling with Ruby on Rails, Part 2”. I build an
application called “mylibrary” using command “rails mylbirary”. Made
necessary changes in database.yml file under config folder to connect to
the database. Used another command “ruby script\generate scaffold type”
to create both model and controller related classes.
I tried to run the newly created application in browser. The URLs
“http:\localhost:3000\type\new” and “http:\localhost:3000\type\list”
are working. But when I tried to access
"“http:\localhost:3000\type\show”, which displays details of a
particular type, it is throwing below error details.
I thought the application should be working with out making any changes.
If I need to make any changes, I could not find proper documentation on
where and how I should make these changes. I would appreciate if
someone can help me in understanding this and moving further with my
development.
==== Error log ====
ActiveRecord::RecordNotFound in TypesController#show
Couldn’t find Type without an ID
RAILS_ROOT: C:/SOFTWA~1/INSTAN~1/rails_apps/mylibrary/config/…
Application Trace | Framework Trace | Full Trace
I tried to run the newly created application in browser. The URLs
“http:\localhost:3000\type\new” and “http:\localhost:3000\type\list”
are working. But when I tried to access
"“http:\localhost:3000\type\show”, which displays details of a
particular type, it is throwing below error details.
It looks to me like your request is not specifying the id of the type
you wish to show the details for. Instead of
“http:\localhost:3000\type\show”, try using
“http:\localhost:3000\type\show\1”. The general pattern you need to use
when working with a single object is
“http:\localhost:3000<controller><action><id>”.
I created a table called “types” for the application.
I would also advise against the use of the word ‘type’ for a model
class; while not a reserved word, it is a word with special meaning -
when used on an object, it returns the class of that object (although
this use is deprecated), and type is also used by Rails as a special
column/attribute to identify the sub-type of an object when using
single-table inheritance.
I tried to run the newly created application in browser. The URLs
“http:\localhost:3000\type\new” and “http:\localhost:3000\type\list”
are working. But when I tried to access
"“http:\localhost:3000\type\show”, which displays details of a
particular type, it is throwing below error details.
It looks to me like your request is not specifying the id of the type
you wish to show the details for. Instead of
“http:\localhost:3000\type\show”, try using
“http:\localhost:3000\type\show\1”. The general pattern you need to use
when working with a single object is
“http:\localhost:3000<controller><action><id>”.
I created a table called “types” for the application.
I would also advise against the use of the word ‘type’ for a model
class; while not a reserved word, it is a word with special meaning -
when used on an object, it returns the class of that object (although
this use is deprecated), and type is also used by Rails as a special
column/attribute to identify the sub-type of an object when using
single-table inheritance.
Hope this helps,
Adam
Hi Adam,
Thanks for your help. I changed the name of table to “item_types”
and changed the statement “
<%= link_to ‘Show’, :action => ‘show’,
:id => id %>
”, as the original
statement was resulting in forming the URL
“http:\localhost:3000\type\show”. After making the change the
resultant URL returned is “http:\localhost:3000\type\show\1”. But
still I am getting errors. This time the first sentence in the error
log is “Mysql::Error: Unknown column ‘item_types.id’ in ‘where clause’:
SELECT * FROM item_types WHERE (item_types.id = ‘1’)”. I am not sure
why I am getting this error and whether the change I made was correct or
not. I would appreciate if you can provide me any more guidance on
this. Attached is the complete error log. I appreciate your help in
resolving this
It looks like you don’t have an “id” - not “item_type_id” - just plain
old “id” column. Rails works best if tables have “id” as their.
primary key.
It’s recommended you use migrations to build and mange your database
tables - this will automatically create the “id” column for you
(amongst other things).
I think if you change the name of your primary key column to “id”
however I think you’ll move forward…
Cheers, --Kip
On Jul 30, 6:55 am, Gangadhar Moganti <rails-mailing-l…@andreas-
I tried to run the newly created application in browser. The URLs
“http:\localhost:3000\type\new” and “http:\localhost:3000\type\list”
are working. But when I tried to access
"“http:\localhost:3000\type\show”, which displays details of a
particular type, it is throwing below error details.
It looks to me like your request is not specifying the id of the type
you wish to show the details for. Instead of
“http:\localhost:3000\type\show”, try using
“http:\localhost:3000\type\show\1”. The general pattern you need to use
when working with a single object is
“http:\localhost:3000<controller><action><id>”.
I created a table called “types” for the application.
I would also advise against the use of the word ‘type’ for a model
class; while not a reserved word, it is a word with special meaning -
when used on an object, it returns the class of that object (although
this use is deprecated), and type is also used by Rails as a special
column/attribute to identify the sub-type of an object when using
single-table inheritance.
Hope this helps,
Adam
Hi Adam,
Thanks for your help. I changed the name of table to “item_types”
and changed the statement “
<%= link_to ‘Show’, :action => ‘show’,
:id => id %>
”, as the original
statement was resulting in forming the URL
“http:\localhost:3000\type\show”. After making the change the
resultant URL returned is “http:\localhost:3000\type\show\1”. But
still I am getting errors. This time the first sentence in the error
log is “Mysql::Error: Unknown column ‘item_types.id’ in ‘where clause’:
SELECT * FROM item_types WHERE (item_types.id = ‘1’)”. I am not sure
why I am getting this error and whether the change I made was correct or
not. I would appreciate if you can provide me any more guidance on
this. Attached is the complete error log. I appreciate your help in
resolving this
Sorry, I forgot to add the error log. Here it is.
ActiveRecord::StatementInvalid in Item typesController#show
Mysql::Error: Unknown column ‘item_types.id’ in ‘where clause’: SELECT *
FROM item_types WHERE (item_types.id = ‘1’)
RAILS_ROOT: C:/SOFTWA~1/INSTAN~1/rails_apps/mylibrary/config/…
Application Trace | Framework Trace | Full Trace
Also looking further at your model,I suggest you make some other
modifications:
if you have the columns “created_at” and “updated_at” then Rails will
automitically update them with the relevant datetime (or created_on/
updated_on for date columns). So I suggest you change your
modified_date and created_on column names.
Rails is a world where following convention gets you great
productivity gains, and not following convention can cause pain and
anguish - as I am learning very fast.
Cheers, --Kip
On Jul 30, 6:55 am, Gangadhar Moganti <rails-mailing-l…@andreas-
I think if you change the name of your primary key column to “id”
however I think you’ll move forward…
As Kip says, following conventions such as using a primary key field of
‘id’ and naming your model after your database table (e.g. class
ItemType for table ITEM_TYPES) is the path of least resistance…
However, sometimes the conventions don’t suit. In these cases, Rails
usually provides a way of overriding the convention. So instead of
renaming your item_types_id field to id, you could add the following
line to the top of your model file:
as the others quite rightly pointed out you shouldn’t use type as a
model name in Rails. However, I suspect the error you got wasn’t
caused by this, but rather by the fact that you were trying to access:
/type/show
instead of say:
/type/show/5
if you look at your controller it will look something like:
def show @type = Type.find(params[:id])
end
params[:id] in your case will be nil (after all you did not specify
one, right?) so it will basically try to do Type.find(nil) if you fire
up the console, you’ll see that this doesn’t work.
This is an inelegance of how actions map to controllers, and once you
get better and start using map.resources this inelegance will be gone.
Just hang in there.
Also I really recommend you put down the Rolling with Rails article
and go out and buy the Agile Web D. With Rails (Second
Edition!) book. I remember that Rolling with Rails was the first
article about Rails I read. The interesting part is that at the time,
it was pretty much the ONLY article about Rails. Yes, this was almost
two years ago. So buy the book and save yourself some pain.
/Jonas
On 30 Juli, 07:55, Gangadhar Moganti <rails-mailing-l…@andreas-
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.