Question

I have 2 forms, both use Credit Card processing, meaning, they both have same checkout process. This check out form is a partial on both these forms.

so what I am trying to achieve is have a conditional POST URL statement in simple_form_for tag like below

<%= simple_form_for :order, url: task_path(params[:task_id], params[:id] if condition else something_else ) do |f| %>

Is it possible to achieve this in Rails.

Was it helpful?

Solution

Calculate the url first, before calling form_for:

<%
  if <some condition>
    url = "/some/url"
  else
    url = "/another/url"
  end
%>
<%= simple_form_for :order, url: url do |f| %>

This is much more readable and easier to understand/change.

UPDATE: since form_for already has two url options depending on whether the object in question has been saved (ie the value of .new_record?) then you may need to make your if-else logic take this into account too.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top