# Too Simple for Rails?

**URL:** <https://www.ruby-forum.com/t/too-simple-for-rails/186038>\
**Category:** Rails\
**Created:** [March 30, 2010, 7:19pm UTC](https://www.ruby-forum.com/t/too-simple-for-rails/186038 "2010-03-30T19:19:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![partydrone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a6a055/32.png) [@partydrone](https://www.ruby-forum.com/u/partydrone)\
**Post date:** [March 30, 2010, 7:19pm UTC](https://www.ruby-forum.com/t/too-simple-for-rails/186038/1 "2010-03-30T19:19:44Z")

</div>

I want to create a simple app.

On one page, I want a form. This form has a single field for a ticket  
number. When someone enters a ticket number and submits the form, they  
are shown a second page with links to download files. And here is a  
list of other criteria:

- validate ticket numbers (is a number within a certain range)
- “used” tickets are stored in the database
- if a ticket is not in the database, add it
- if a ticket is in the database, increase “count” field by one

This seems simple enough, but I don’t quite understand how to take all  
of rails discrete processes (create using POST, update using PUT, show  
using GET, all in separate actions within a controller) and roll it  
all up into a single controller (or put it in the model, which makes  
sense).

Should I scrap trying to use ActiveRecord and go to straight Ruby for  
this? I’ve never done database access straight from Ruby before.

Here’s the code: [GitHub - partydrone/ticket\_check: Allow ticket purchasers to redeem a concert ticket for a file download.](http://github.com/partydrone/ticket_check)

Any suggestions would be helpful. Thank you.

---

<div class="post-metadata">

**Author:** ![partydrone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a6a055/32.png) [@partydrone](https://www.ruby-forum.com/u/partydrone)\
**Post date:** [March 30, 2010, 11:03pm UTC](https://www.ruby-forum.com/t/too-simple-for-rails/186038/2 "2010-03-30T23:03:15Z")

</div>

Hi Andrew,

I don’t think this is too simple to use for Rails. You may want to  
consider using Sinatra or a smaller web framework, but I wouldn’t  
write it in straight Ruby unless you want that experience.

As for how to do this in Rails, usually when you have a form it should  
map to the “new” or “edit” controller actions. Here you are creating  
tickets. The only exception is that if the ticket already exists in  
the database you want to update the existing record. However, this  
behavior still belongs in the create action because, from the  
interface perspective, the user is still creating a ticket.

Whenever you’re deciding whether a request should be POST (create) or  
PUT (update), ask yourself this: if the user submits the same request  
again, will the second request alter the database? When updating a  
record you can submit the PUT request as many times as you like  
without effecting the database much. In contrast if you submit a POST  
request multiple times it will create multiple records. Here you’re  
incrementing a counter so the database changes each time the request  
is submitted, therefore a POST (create) request is the right way to  
go.

The create action might look like this.

def create  
@ticket =  
Ticket.find\_or\_initialize\_by\_number(params[:ticket\_number])  
@ticket.increment(:count) unless @ticket.new\_record?  
if @ticket.save  
redirect\_to @ticket  
else  
render :new  
end  
end

Everything else would be standard RESTful controller actions.

Hope that helps,

Ryan

---

<div class="post-metadata">

**Author:** ![partydrone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a6a055/32.png) [@partydrone](https://www.ruby-forum.com/u/partydrone)\
**Post date:** [March 31, 2010, 9:11am UTC](https://www.ruby-forum.com/t/too-simple-for-rails/186038/3 "2010-03-31T09:11:13Z")

</div>

Actually, I believe this is even simpler than you think.

Why don’t you have a process which is:

People enter the ticket number  
After validation they are directed to the download page.

You don’t need to check if the ticket is in the database, you add it  
anyway.  
Later, you’ll be able to list the tickets entered and to count them.

if you use the scaffold, you have the new page that takes care of  
entering the ticket  
This should use the create action in which you put the validation code  
and make sure that it redirects to the download page  
The index page can be tuned to display the list of tickets and their  
respective count

You don’t need the update page nor delete page

Doesn’t seem a big deal to me

Christophe

Le 30 mars 2010 à 19:18, partydrone a écrit :
