Place 97
Place 97

Reputation: 41

Problem with radio button tag ruby ​on rails

I am developing a simple account registration and update form and I have recently entered a parameter that can have two possible choices, in my case one problem occurred:

  1. Problem:
  <div class="tag">
      <%= f.label :ruolo %><br />
      <%= f.radio_button_tag(:ruolo, "admin") %>
      <%= f.label_tag(:ruolo_admin, "admin") %>
      <%= f.radio_button_tag(:ruolo, "user")  %>
      <%= f.label_tag(:ruolo_user, "user") %>  
  </div>

if I leave the code like this it gives me an error like: NoMethodError in Devise::Registrations#edit

undefined method `radio_button_tag 'for # <ActionView :: Helpers :: FormBuilder: 0x00007fd6fd02f478>

this is the form:

<div class="row">
  <div class="col s12 l6 offset-l3">
    <br>
    <br>
    <h4 class="center">Registrazione</h4>

    <br>

    <%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
    <%= devise_error_messages! %>

    <div class="field">
      <%= f.label :name %><br />
      <%= f.text_field :name, autofocus: true %>
    </div>

    <div class="field">
      <%= f.label :email %><br />
      <%= f.email_field :email %>
    </div>

    <div class="field">
      <%= f.label :ruolo %><br />
      <%= f.radio_button_tag(:ruolo, "admin") %>
      <%= f.label_tag(:ruolo_admin, "admin") %>
      <%= f.radio_button_tag(:ruolo, "user") %>
      <%= f.label_tag(:ruolo_user, "user") %>  
    </div>

    <div class="field">
      <%= f.label :password %>
      <% if @minimum_password_length %>
      <em>(<%= @minimum_password_length %> characters minimum)</em>
      <% end %><br />
      <%= f.password_field :password, autocomplete: "off" %>
    </div>

    <div class="field">
      <%= f.label :password_confirmation %><br />
      <%= f.password_field :password_confirmation, autocomplete: "off" %>
    </div>

    <br>

    <div class="actions">
      <%= f.submit "Iscriviti", class: "waves-effect waves-light btn btn-devise" %>
    </div>

    <br>

  <% end %>

  <%= render "devise/shared/links" %>
  </div>
</div>

Controller code:

class ApplicationController < ActionController::Base

  before_action :configure_permitted_parameters, if: :devise_controller?
  
  protected


  

  def configure_permitted_parameters
    @roles=Role.all
    devise_parameter_sanitizer.permit(:sign_up, keys: [:name,:ruolo])
    devise_parameter_sanitizer.permit(:account_update, keys: [:name,:ruolo])
  end

end

Upvotes: 0

Views: 407

Answers (1)

fabriciofreitag
fabriciofreitag

Reputation: 2883

radio_button_tag is not an instance method of ActionView :: Helpers :: FormBuilder, but rather ActionView::Helpers::FormTagHelper

So instead of calling it like:

<%= f.radio_button_tag(:ruolo, "admin") %>

You simply call it like:

<%= radio_button_tag(:ruolo, "admin") %>

For more details on this method please check the docs: https://apidock.com/rails/v6.0.0/ActionView/Helpers/FormTagHelper/radio_button_tag

Upvotes: 2

Related Questions