Html form and form_for

So, i’m not sure exactly how to do this, or if it is a smart thing to do
this. I need some help on the best way to approach this.

I have a simple form, it has a text_field and a text_area. I want to add
2 submit buttons. Each one will do a different action. I was thinking I
could do form_for, and fields_for, but then I thought that fields_for is
used for having two objects, but I only have one. I know I could just
make them images, and links, but I suck at photoshop, and my boss is one
of those people that feels he is a designer, and he tells me that I have
to have some sort of flashing animated gif that is on the right hand
side, and some link that has to be red over on the left side. He also
told me I have to have two buttons, but I don’t know how to do that.

<% form_for(:trouble_ticket, @ticket, :url => { update_url }) do |f| %>

Repaired by:
<%= f.text_field(:repair_tech_name) %>

Comments:
<%= f.text_area(:comments) %>

<%= submit_tag('Close ticket') %> or <%= submit_tag('Assign Ticket') %>

<% end %>

Thanks,

~Jeremy

This is just me, but I’d make them both buttons and assign a
javascript function to the onclick events. In the function, I’d grab
the form via prototype, set the action accordingly, then submit it.
There’s probably a more Rails-pure way of doing it, but I generally
go with what works first.

Peace,
Phillip

Phillip K. wrote:

This is just me, but I’d make them both buttons and assign a
javascript function to the onclick events. In the function, I’d grab
the form via prototype, set the action accordingly, then submit it.
There’s probably a more Rails-pure way of doing it, but I generally
go with what works first.

Peace,
Phillip

that sounds awesome, unfortunately my javascript skills suck, but I will
see what I can come up with doing it that way. Thanks for the advice.

~Jeremy

Jeremy W. wrote:

Phillip K. wrote:

This is just me, but I’d make them both buttons and assign a
javascript function to the onclick events. In the function, I’d grab
the form via prototype, set the action accordingly, then submit it.
There’s probably a more Rails-pure way of doing it, but I generally
go with what works first.

Peace,
Phillip

that sounds awesome, unfortunately my javascript skills suck, but I will
see what I can come up with doing it that way. Thanks for the advice.

~Jeremy

here is a simple javascript call from an onclick:

THIS IS A BUTTON IN THE VIEW RHTML FILE:

THE BUTTON NAME

PUT THIS IN THE HEADER SECTION OF THE RHTML

<script type='text/javascript"> function mycoolfuction(){ alert('I BAD. I BAD. YOU KNOW IT!'); }

now, for you easy javascript pleasure, go here,
and you will love all this javascript all over the place:

http://www.w3schools.com/js/default.asp

i did not get into server side junk, and i am not sure you need it.

Hi Jeremy,

Sorry to state the obvious here, but there’s only one way for your JS
skills to get better…code it. I’ve been writing JS for less than a
year, and while I don’t have the knowledge or skills to work on
Prototype or Scriptaculous (or any other wonderful library), I can
look at it and understand it (for the most part). Heh, I remember
the first little JS thing I did. I was “Oh! That’s so cool!” But you
have to start somewhere.

For this purpose, your code is going to look something like:


function close_ticket()
{
var f = $(‘ticket_form’);
f.action = ‘url to closing ticket’;
f.submit();
}

function assign_ticket()
{
var f = $(‘ticket_form’);
f.action = ‘url to assigning ticket’;
f.submit();
}

Assuming you have no special logic for either closing or assigning,
you can make this a single function and pass in some value that
indicates what you want to do:

function submit_form(action)
{
var = $(‘ticket_form’);

if (action == ‘close’)
{
f.action = ‘url to close’;
}
else
{
f.action = ‘url to assign’;
}

f.submit();
}

then both buttons would call the same function:


I haven’t tried this code, so there might be some holes, but the
theory is sound. There are also other ways you could change it here
and there to make it more compact and dry. I’ll leave that to you.

Ahh…after taking from the community for so long, it’s good to
finally be able to give something back.

Peace,
Phillip

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