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

On Mar 30, 10:18 am, partydrone <[email protected]> wrote:
> 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:http://github.com/partydrone/ticket_check
>
> Any suggestions would be helpful. Thank you.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to