tkrishnan
tkrishnan

Reputation: 327

Simple form in Rails Without Database

I just started trying to get acquainted with Rails, and I don't really know much Ruby. Currently I'm doing a beginners project to get acquainted with the framework which involves a simple form which takes a name, email, and phone number, and when you hit a submit button the page should refresh and present the information you submitted (so there is no database interaction and the model is supposed to do very little). It's very simple, but as my current knowledge of Ruby is pretty minimal, I'm getting somewhat confused. I've written the views for the most part but I'm still confused on what to put in the controller and the model. If anyone could provide any hints that would be great!

Upvotes: 1

Views: 2415

Answers (3)

Anil
Anil

Reputation: 3919

It is much easier with a database.

rails new stack
cd stack
rm public/index.html
rails generate scaffold Member name:string email:string phone:string
rake db:migrate
rails server

Then browse to localhost:3000/members

(Assuming you are using rails 3)

Upvotes: 1

Firoz Ansari
Firoz Ansari

Reputation: 2515

You need tableless model. Please refer excellent webcast from Ryan on this topic.

http://railscasts.com/episodes/193-tableless-model

Upvotes: 3

Carl Zulauf
Carl Zulauf

Reputation: 39558

Models (at least those descending from ActiveRecord::Base) require a database, so if you are using a model you are using a database, even if its a simple one like SQLite.

If you don't want to descend into models and generating a migration to create the tables for your models, then you are probably just going to store the form values into instance variables and reference those in your view. Below is a quick example how to do that.

If you have a form that sends data (ex: name and email) to a create action, the create action would look sort of like this:

def create
  @name = params[:name]
  @email = params[:email]
  render :my_template
end

The create action above is assigning the params sent to it to instance variables, which you can then reference in your view. In the above example the create action is going to try and render a view called my_template which would probably be named my_template.html.erb and might look something like this:

<p>Your name is <%= @name => and your email is <%= @email =>.</p>

This is an extremely small and contrived example, but hopefully this helps.

When you move on to working with models your create action might instead create a new instance of a model, pass that model the params sent by the user, save the model, and then redirect them to a page that shows the data.

Upvotes: 2

Related Questions