Search code examples
javascriptruby-on-railsrubyajaxransack

How to dynamically change url or action in form dependent on selected field?


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.


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}")