Rails - changing the form of action based on the choice of radio

I am Rails noob. I will not lie. I was instructed to take two forms and turn them into one dynamic form. This is a login / registration form. The only problem I am facing is how to handle part of the Rails code. Using jQuery, I could easily replace the actions of the HTML form, but how could I approach replacing the action / destination in the rails code based on the choice of radio input or registration.

<%= form_tag login_path, :id => '_login_form' do %> <% end %> <%= form_tag sign_up_path, :id => '_sign_up_form' do %> <% end %> 
+4
source share
1 answer

I see two options for solving your problem.

Using javascript, you can change the action of the form based on the selected radio button.

 $("#radio").change(function() { var action = $(this).val() == "some_value" ? "login" : "sign_up"; $("#your-form").attr("action", "/" + action); }); 

Or you can process both methods in one action and process each of the options separately

 #view <p> <%= radio_button_tag :option, "login" %> Orange </p> <p> <%= radio_button_tag :option, "sign_up" %> Peach </p> #controller if params[:option] == "login" #do login elsif params[:option] == "sign_up" #do sign up end 

Hope this helps!

+8
source

Source: https://habr.com/ru/post/1445523/


All Articles