我正在使用rails4.2.0 在我的电子商务网站中,当用户提交他的付款类型时,我想重定向外部网站。流程如下。
2和3的来源就像,
create.html.erb
<%= form_for(:user,:url => { controller: "settlements", action: "settlement"}, remote: true, html: {class: :settlement_form}) do |f| %>
<%= f.radio_button :settlement_type, 0 %>paypal
<%= f.radio_button :settlement_type, 1 %>credit card
<%= f.hidden_field :email, :value => @user.email %>
<%= f.hidden_field :fee_type, :value => @user.fee_type %>
<%= f.submit "Submit", data: { disable_with: "Please wait..." }, class: "btn btn-warning" %>
settlement_controller
def settlement
user = User.new(user_params)
if user.save
# parameters for outside website
@payment_params
else
render "new"
end
end
settlement.js.erb
var form = $('<form></form>',{id:"pay",action:'http://outside_ec_site_url/hoge',method:'POST'}).hide();
var body = $('redirect');
body.append(form);
form.append($('<input/>', {type: 'hidden', name: 'something', value: <%= @payment_params[:something] %>}));
form.append($('<input/>', {type: 'hidden', name: 'something', value: <%= @payment_params[:something] %>}))
form.submit();
答案 0 :(得分:0)
我解决了这个问题,但我想知道更好的方法。
更改了settlement.js.erb,
$("#redirect").html("<%= escape_javascript(render :partial => 'pay' ) %>");
并创建了新文件_pay.html.erb
<script>
$('<form/>', {id: 'paygent',action: "outside_website_url", method: "POST"})
.append($('<input/>', {type: 'hidden', name: 'something1', value: "<%= @payment_params[:somthing1] %>"}))
.append($('<input/>', {type: 'hidden', name: 'something2', value: "<%= @paygent_params[:something2] %>"}))
.appendTo(document.body)
.submit();
</script>
然后,它的工作原理。 你知道其他方法吗?任何想法都是适用的。