开发者

submit_tag do hidden_field - saving data?

I'm creating simple p开发者_如何学JAVAayment form that allows users to select from several different gateways to pay (paypal, google checkout, etc).

All the gateways will be on their hosted page, not integrated on my own.

What I'm trying to do logically, is create several different submit buttons on my form. Each submit button then would have a hidden_field that specifies a gateway, something like:

<%= f.submit "Payment", :type => :image, :src => "https://www.paypal.com/en_US/i/btn/btn_xpressCheckout.gif"  do %>
<% f.hidden_field :gateway, :value => "paypal" %>
<% end %>

Then in my controller:

if @payment.save and @payment.gateway == "paypal"
      redirect_to @payment.paypal_url #model method to link to payment url
else
      render "new"
end

However, I'm having issues with the hidden_field data saving. Everytime I'm coming up with a nil value for my gateway.

I guess I have two questions:

How do I assign data with a hidden_field? Can a submit_tag be used for a do loop with a hidden_field?


I think the better solution will be using javascript to handle filling hidden fields. So you can do something like this:

<%= hidden_field_tag "gateway", :id => "gateway" %>
<%= submit_tag "Paypal", :id => "paypal", *other options* %>
<%= ... other similar buttons %>

Then you can use javascript to fill hidden_field_tag with nessesary data: say name of gateway(paypal, ...). For this just find DOM element (hidden_field_tag) by id: gateway and fill with your data.
For. ex:

$(function(){
  $('input[type='submit']').click(function(){
     var gateway_title = $(this).attr("id")
     $('#gateway').val(gateway_title);
  })
})

..or you can set such code on form submit handler.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜