Question

How to dynamically change url dependent on selected field?

= search_form_for @q, url: offers_path, builder: SimpleForm::FormBuilder do |f|
  = f.input :name_cont
  = f.input :time_type_id_eq,
    collection: [['Promotion', 'temp'], ['Permanent', 'permanent']], 
    include_blank: false
  = f.submit

If form will have selected temp option in time_type_id_eq I would like to set url or action for this form to temporary_offers_path analogously for permanentchange path to permanent_offers_path

class OffersController < ApplicationController

  def index
    @temporary_offers = Offer.temporary
    @permanent_offers = Offer.permanent
    @recommended_offers = Offer.recommended
  end

  def temporary
    @q = Offer.search(params[:q])
    @temporary_offers = @q.result(distinct: true).temporary
  end

  def permanent
    @q = Offer.search(params[:q])
    @permanent_offers = @q.result(distinct: true).permanent
  end
end

I am using Ransack as searcher.

Was it helpful?

Solution

This coffescript example might be useful:

  $("#submit_search_form").click (e) ->
    search_type = $('select#search_type').val();
    if search_type == 'Client'
      $("#search_form").attr("action", "/orders/#{order_id}")

OTHER TIPS

I will Use Check boxes in this case.And by using JavaScript and HTML.

Firstly Assuming your Form name as myform and by using HTML forms

<form name="myform" onsubmit="return OnSubmitForm();">

   <input type="radio" name="Promotion" value="1">Temporary
   <input type="radio" name="Promotion" value="2">Permanent
   <p>
   <input type="submit" name="submit" value="save">
   </p>
</form>

JavaScript:

<script type="text/javascript">
function OnSubmitForm()
{
  if(document.myform.Promotion[0].checked == true)
  {
    document.myform.action ="temporary.html";
  }
  else
  if(document.myform.Promotion[1].checked == true)
  {
    document.myform.action ="permanent.html";
  }
  return true;
}
</script>

NOTE: This is just a Sample Code for your understanding

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