Can someone explain something to me?

So this is a question that I keep trying to think through in my head
and it’s the first time I’ve had to implement it in an application. I
come from a Visual Foxpro background and when I would want to add a
button to a form I could simply place it on the form and then modify
the clickevent with my custom code for that button.

In rails if I’m working on the New or Edit form I want to add a
button that finds the last record in the table, take the account
number, and then increment it by one and then replace the contents of
the account field on my form with that new number. Here’s my
question…

Do I add a separate method in my controller simply for this button (ie
increment_account) or something like that? If I do this, how do I call
that from my view? Do I always call it within a form by itself? This
are fairly general questions, and probably ones I should know the
answer to, but I’m finding that I’m not entirely sure how to implement
little buttons that do special things within the “new” or “edit”
views. If someone has the time to give me an example that would be
awesome . I just want to know the proper way to implement something
like this…

Thanks!

Chris

Hello Chris:

I understand what you are attempting to do, but not sure the reasoning
behind it. In regards, to a new or edit forms, I would be incrementing
the account number when the form is posted. It might be worth looking
at the interface patterns developers use for ROR web applications
rather than thinking in terms of desktop Foxpro patterns.

However having said that, to add additional methods, in a RESTFul
world I probably add it as another resource/controller. For example,
to increment an account number I would create a controller called
AccountNumbersController (i.e. for the resource account_number) with
an update method that does the increment.

But without knowing your domain model this might be bad advice.

As a last resort, you can always add it as another method in the
controller, but I like to keep the controllers to the REST methods,
and identify the resource I’m attempting to update and create a
controller for that.

Cheers,
Nicholas

On Sep 2, 1:57 pm, internetchris [email protected]

So what happens if 15 people click on ‘New’ at the same time? Do they
all get the same “new” account number displayed ?

Firstly it is better to try and use a subject line that indicates
something about the problem.

2009/9/2 Nicholas H. [email protected]:

world I probably add it as another resource/controller. For example,
to increment an account number I would create a controller called
AccountNumbersController (i.e. for the resource account_number) with
an update method that does the increment.

But without  knowing your domain model this might be bad advice.

As a last resort, you can always add it as another method in the
controller, but I like to keep the controllers to the REST methods,
and identify the resource I’m attempting to update and create a
controller for that.

If you decide that a button to perform that action is what your UI
requires then the button should submit a POST to an action of one of
the controllers. It should be a POST as you are modifying the
database and you do not want google accidentally GETing the URL that
performs this action. You could have effectively an empty form with a
submit button to so this, but I expect there is a way of doing it
without a form, though I have never done this. Alternatively your
main form can have multiple submit buttons so the action will go to
the same action as your normal submit button but you can identify in
the action which button has been pressed. The button id or name or
something gets passed in the params. Look in the log at what happens
when you press a button and you will see it. Since the action you
describe does not seem to have anything to do with the main purpose of
the form however this does not seem like the best approach. However
you submit it, once the action is complete then display the original
view again.

Colin

internetchris wrote:
[…]

In rails if I’m working on the New or Edit form I want to add a
button that finds the last record in the table, take the account
number, and then increment it by one and then replace the contents of
the account field on my form with that new number.

No you don’t. You want to define an autoincrementing account number in
the database and let the database pick the next unused number. (In fact,
you can use the id field for this.)

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

internetchris wrote:

I come from a Visual Foxpro background and when I would want to add a
button to a form I could simply place it on the form and then modify
the clickevent with my custom code for that button.

What’s primarily tripping you up in your thinking has to do with the
difference between event-driven vs. request-response driven systems.
Foxpro is an event-driven system where an event loop constantly runs.
The loop monitors an event queue and responds to those events as they
arrive. This is not the case with web based applications, regardless of
the framework used to build them (apart from AJAX that is).

Web applications work on a request-response cycle:

  1. A user requests a form (GET /accounts/new)
  2. The server responds with HTML representing the account form
  3. The server continues processing other requests
    … time passes
  4. The user submits the form (POST /accounts)
  5. Rails routing system associates the request based on the HTTP method
    (POST) and URL (/accounts)
  6. Rails calls the “create” method of accounts_controller
  7. Server response with HTML depending on the “create” action.
  8. Server waits for another request.

This is also referred to as “stateless” communication. That’s how the
web was designed. The notion of state was added onto HTTP later by using
cookies and other methods of identifying user state.

Rather than thinking of the button as “having an event” it’s really the
form itself that has the event (technically the “action” of the form).
The button is just a visual widget to provide the user a way to submit
the form.

internetchris wrote:

Robert, that was a great explanation! I appreciate all of the other
replies too. A couple more question to make sure I understand
correctly…

So in all honesty this little button doesn’t have much to do with the
overall process, but it makes it easier for the user to see the last
account number used so they can assign the next available account when
submitting new records.

The user should probably not be assigning account numbers. That’s a job
for the DB.

New records will be infrequent so the idea
that more than one user is posting them is probably unlikely but still
possible. I can check for this prior to saving the record.

Just avoid the problem entirely by letting the DB supply the number!

Robert, to make sure I understand what you are saying let me summarize
my understanding…

The form submission “in it’s entirety” is considered the “event” - the
only way to provide the user with information like what I want to do
is either when the page loads, via a refresh (Post) of some sort, or a
tricky ajax event. It’s not possible to call little events via buttons
or forms when dealing with a web application unless I use ajax - is
that correct?

Not quite. You can have as many forms or links to controller actions as
you like on a given page. However, absent JavaScript, each form can
only have one submit action.

I really appreciate the help - this is something I have been grappling
with in my brain. It’s simply a hold over from how I learned to
program - for better or worse.

Thanks!

Chris

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Marne,

Thanks for the advice - I implemented an auto-incrementing number that
the database assigns - much cleaner. My thought process was left over
from a previous app I used to work with. I just needed some help
thinking outside that box. I can auto-increment it and if the user
doesn’t like it they can change it if they want.

Thanks!

Chris

On Sep 2, 5:45 pm, Marnen Laibow-Koser <rails-mailing-l…@andreas-

Robert, that was a great explanation! I appreciate all of the other
replies too. A couple more question to make sure I understand
correctly…

So in all honesty this little button doesn’t have much to do with the
overall process, but it makes it easier for the user to see the last
account number used so they can assign the next available account when
submitting new records. New records will be infrequent so the idea
that more than one user is posting them is probably unlikely but still
possible. I can check for this prior to saving the record.

Robert, to make sure I understand what you are saying let me summarize
my understanding…

The form submission “in it’s entirety” is considered the “event” - the
only way to provide the user with information like what I want to do
is either when the page loads, via a refresh (Post) of some sort, or a
tricky ajax event. It’s not possible to call little events via buttons
or forms when dealing with a web application unless I use ajax - is
that correct?

I really appreciate the help - this is something I have been grappling
with in my brain. It’s simply a hold over from how I learned to
program - for better or worse.

Thanks!

Chris

On Sep 2, 2:19 pm, Robert W. [email protected]

So I can basically place “if” statements in the action based upon the
identification of the submit button right? That seems pretty easy
although I could see how this might be messy if a user has filled out
another part of the form and you want to make sure that information is
either saved prior to them using another type of submit button. Can
all of this be circumvented using ajax within the form? With the use
of Ajax can I submit a single call to an action without worrying about
anything else on that form?

Thanks

Chris

2009/9/3 Marnen Laibow-Koser [email protected]:

Robert, to make sure I understand what you are saying let me summarize
you like on a given page. Â However, absent JavaScript, each form can
only have one submit action.

Just a small clarification on this, each form can have only one submit
action but it can have multiple buttons that call the action. So
you can, for example have two buttons labelled Do it and Cancel using
<%= submit_tag “Do it” %>
<%= submit_tag “Cancel” %>
and then in the action determine which button was pressed by examining
params[:commit]

Colin

internetchris wrote:

So I can basically place “if” statements in the action based upon the
identification of the submit button right?

Yes. You could have a dispatcher in the controller that does this,
although I do not recommend doing this frequently – it may be messy.
Remember that you can use multiple forms or plain old links.

That seems pretty easy
although I could see how this might be messy if a user has filled out
another part of the form and you want to make sure that information is
either saved prior to them using another type of submit button.

Uh, what? You only get one submission at a time.

Can
all of this be circumvented using ajax within the form? With the use
of Ajax can I submit a single call to an action without worrying about
anything else on that form?

Sure. But of course it won’t work if the client has JS turned off.

Thanks

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Chris

Many thanks to everyone, my questions have been satisfied.

On Sep 3, 11:48 am, Marnen Laibow-Koser <rails-mailing-l…@andreas-

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