All threads / How to pass the value of input type radio in application_helper?

Ask A Question

Notifications

You’re not receiving notifications from this thread.

How to pass the value of input type radio in application_helper?

theshashiverma asked in Rails

I am creating an admin template, and there is a customizer section in which I have created 3 radio buttons (named: Red, Blue, Green). There is a left sidebar navigation menu and I want to change the background color of sidebar navigation menu by clicking on the radio buttons. If I am clicking on the button it is changing the bg-color of sidebar navigation menu by adding a class, but if I am going to another page gets reloaded and the class gets removed.
I have tried one thing, in my application_helper.rb I have added the following code

    def is_active(action)
        @color = "red"
        params[:action] == action_name ? @color : nil                  
      end

and in my layouts folder, I have a partial file _sidebar.html.erb and at the outer div I am using this helper like below

    <div class="sidebar-nav <%= is_active('action_name') %>" id="collapse-sidebar"  >

What I am doing here is, checking the action name if it is matching then adding a class red which is stored in an instance variable @color

If I can pass the value of the radio button to the application_helper file dynamically means if I click on red the value should be red, green then the value should be green. and by which I can change the class name dynamically in application helper and further that would change the color of the sidebar.

Please, If I am doing anything wrong or it's is not the right way to do this or we can't achieve this kind of functionality in ruby on rails, please acknowledge me, that would be appreciable.

On stackoverflow:
https://stackoverflow.com/questions/55808182/how-to-pass-the-value-of-input-type-radio-in-application-helper

Thank you

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 33,399+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.