How do I form a Rails link_to with custom field value as parameter
Posted
by
rwheadon
on Stack Overflow
See other posts from Stack Overflow
or by rwheadon
Published on 2012-12-14T23:02:05Z
Indexed on
2012/12/14
23:03 UTC
Read the original article
Hit count: 172
I have an invoice form where I'm giving the user opportunity to apply coupons to the invoice total. These coupons are held in another Model and I am going to do a lookup on the Coupon code (something like "20OFFONFRIDAY") which I will use to find what the restrictions and benefits of the coupon. (and to see if it even exists at all)
The invoice does not have "coupon_code" on it so I hand forged the field onto my form with html:
<% if (@invoice.status == 'new') %>
<input id="coupon_code" name="coupon_code" type="text"/>
<% end %>
and I am calling a controller method with link_to and would like something like the following jquery enhanced link_to to work:
<%= link_to "Apply Coupon", {
:controller=>"invoices",
:id=>@invoice.id,
:coupon_code=>$('.coupon_code').val(),
:action=>"apply_coupon_code"
},
:method=>"post" %>
^formatted for easier reading
Then inside my "apply_coupon_code" method I will go off to a couple other models and perform business logic before returning the updated invoice page.
...but maybe it's a pipe dream.
I guess if push came to shove I could add the "coupon_code" field to my invoice model (even though it's persisted elsewhere.) so it's part of the entity and thus easily available on my form to send back into a controller, but I just hate adding a column to make a coupon validation easier.
I figured I'd ping stackoverflow before taking that path.
© Stack Overflow or respective owner