user11682335
user11682335

Reputation:

Conditional Form Simple Form

I have a simple_form (only a search text input) with a predefined route. However, I would like to add two radio buttons on the top of it, for example one named "Product" and another one named "Profile". And whenever the user clicks on the "Product" button; and that button is active, then the form routes to the product_index and same logic with the profile (to profile_index). How can I pass a JS argument (class active / not active) to the route of the form (erb)? Thanks!

Upvotes: 0

Views: 218

Answers (2)

Kiran Mahale
Kiran Mahale

Reputation: 173

Your form contains a action attributes and you can change that action using js on selecting "Profile" or "Product". You need a onchange event for radio buttons and when changed then change url in action attributes. Ex.

function onChangeRadio( action ) {
 if( action == "Product" ) {
   $("form").attr("action","/products";
 }
 else if( action == "Profile" ) {
  $("form").attr("action","/profiles";
 }
}

Upvotes: 0

Sanjay Prajapati
Sanjay Prajapati

Reputation: 834

When you select "Product", you can set value in hidden field using JS along with search input and pass that value to contoller#action as params. Now,In controller action check that hidden field value. If value is product then route to product_index if value is profile then route to prodile_index

Upvotes: 0

Related Questions