Too Simple for Rails?

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.

Any suggestions would be helpful. Thank you.

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

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