Connect Rails to an existant database

Hello,

I’ve been using rails for a few days and I know how to create a new
application and new database.

My problem is that I now have to connect Rails to an existant
database, and I don’t know how to do …

Can somebody help me please ?

Thanks :slight_smile:

I’m nowhere near positive, but you might want to give this a try.
Go into your app’s config folder and find the database.yml file. Open
it up with SciTE. You should see:

development:
adapter: (mysql or something)
database: (db name)
username:
password:
host:

Warning: The database defined as ‘test’ will be erased and

re-generated from your development database when you run ‘rake’.

Do not set this db to the same as development or production.

test:
adapter:
database:
username:
password:

production:
adapter:
database:
username:
password:

Fill in the development area with the info to the existing database.
That… should be it I think.

Fill in the development area with the info to the existing database.
That… should be it I think.

Yes, that’s what I’ve done :

development:
host: 127.0.0.1
adapter: postgresql
database: RAMU_florian
username: florian
password: florian

But it needs some views rhtml pages to work…
This pages are normally generated by script/generate scaffold, but
when I try to generate a scaffold, the table already exists on the
database so there’s an error…

Connecting to an existing database is failrly straight forward. Open
the database.yml file and fill in your database parameters in the
development section.

adapter should be ‘mysql’ or ‘postgresql’ etc (depending on the
database you use)
database should be the name of the database you want to connect to.
also give the username and password.

you are done with the connection part.

To actually use the data fro existing tables, you can create models
with appropriate name (if table name is users, you should have a Model
with name User)

OR

create a model and use the “set_table_name”

Eg:

class Foo < ActiveRecord::Base
set_table_name “project”
end

To actually use the data fro existing tables, you can create a model and use the “set_table_name”

Eg:

class Foo < ActiveRecord::Base
set_table_name “project”
end

I’ve followed what you’ve said, but it doesn’t work :s
When I try to go on http://127.0.0.1:3000/correspondant (after having
put ‘set_table_name “correspondant”’ in correspondant.rb), this error
appears :

Unknown action
No action responded to index

I think it does because ‘views’ RHTML pages aren’t generated…
No ?

not exactly. this error arises because you dont have an “index” method
defined in the Correspondant controller. you need to define this
method, as well as write an index.rhtml for the same, in the views/
correspondant.

not exactly. this error arises because you dont have an “index” method
defined in the Correspondant controller. you need to define this
method, as well as write an index.rhtml for the same, in the views/
correspondant.

So, I’ve copied the content of C:\Documents and Settings\Administrateur
\ramu\app\controllers\correspondant_controller.rb from a previous file
and did the same for index.rhtml, but it doesn’t work…
The error is :

Expected C:/Documents and Settings/Administrateur/ramu/app/controllers/
correspondant_controller.rb to define CorrespondantController

Whereas this file exists !

Here is its content :

class CorrespondantsController < ApplicationController

GET /correspondants

GET /correspondants.xml

def index
@correspondants = Correspondant.find(:all)

respond_to do |format|
  format.html # index.html.erb
  format.xml  { render :xml => @correspondants }
end

end

GET /correspondants/1

GET /correspondants/1.xml

def show
@correspondant = Correspondant.find(params[:id])

respond_to do |format|
  format.html # show.html.erb
  format.xml  { render :xml => @correspondant }
end

end

GET /correspondants/new

GET /correspondants/new.xml

def new
@correspondant = Correspondant.new

respond_to do |format|
  format.html # new.html.erb
  format.xml  { render :xml => @correspondant }
end

end

GET /correspondants/1/edit

def edit
@correspondant = Correspondant.find(params[:id])
end

POST /correspondants

POST /correspondants.xml

def create
@correspondant = Correspondant.new(params[:correspondant])

respond_to do |format|
  if @correspondant.save
    flash[:notice] = 'Correspondant was successfully created.'
    format.html { redirect_to(@correspondant) }
    format.xml  { render :xml => @correspondant, :status

=> :created, :location => @correspondant }
else
format.html { render :action => “new” }
format.xml { render :xml => @correspondant.errors, :status
=> :unprocessable_entity }
end
end
end

PUT /correspondants/1

PUT /correspondants/1.xml

def update
@correspondant = Correspondant.find(params[:id])

respond_to do |format|
  if @correspondant.update_attributes(params[:correspondant])
    flash[:notice] = 'Correspondant was successfully updated.'
    format.html { redirect_to(@correspondant) }
    format.xml  { head :ok }
  else
    format.html { render :action => "edit" }
    format.xml  { render :xml => @correspondant.errors, :status

=> :unprocessable_entity }
end
end
end

DELETE /correspondants/1

DELETE /correspondants/1.xml

def destroy
@correspondant = Correspondant.find(params[:id])
@correspondant.destroy

respond_to do |format|
  format.html { redirect_to(correspondants_url) }
  format.xml  { head :ok }
end

end
end

I’m becoming crazy =(

your class needs to be named CorrespondantController and not
Correspondant’s’Controller

your class needs to be named CorrespondantController and not
Correspondant’s’Controller

This means I have to modify what files ?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs