ejoka
ejoka

Reputation: 111

Ruby on Rails Form is not Capturing Values of Radio Button

I have a form in the Ruby on Rails app, It has four radio buttons, three of the radio buttons have default values i.e 25, 50, and 75, the fourth radio button does not have a value, however when you click it, it displays an input field for entering any number, The form only picks the value of the input field but not the radio button values. I want the form to capture values that are checked in any of the radio buttons.

here are my form details:

<div class="row__form-control">
    <div class='row__form-control_amount'>
      <%= form.radio_button :amount, 25, class: "radio_button25", :onclick=>"notification()" %>
      € <%= form.label :amount, 25 %>
    </div>
    
    <div class='row__form-control_amount'>
      <%= form.radio_button :amount, 50, class: "radio_button50", :onclick=>"notification()" %>
      € <%= form.label :amount, 50  %>
    </div>

    <div class='row__form-control_amount'>
      <%= form.radio_button :amount, 75, class: "radio_button75", :onclick=>"notification()" %>
      € <%= form.label :amount, 75 %>
    </div>

    <div class='row__form-control_amount'>
      <%= form.radio_button :amount, 0, class: "radio_custom", :onclick=>"custom()" %> 
      <%= form.label :custom %>
    </div>
  </div>

Here is my controller:

before_action :set_donation, only: %i[ show edit update destroy ]

  # GET /donations or /donations.json
  def index
    @donations = Donation.all
  end

  # GET /donations/1 or /donations/1.json
  def show
  end

  # GET /donations/new
  def new
    @donation = Donation.new
  end

  # GET /donations/1/edit
  def edit
  end

  # POST /donations or /donations.json
  def create
    @donation = Donation.new(donation_params)

    respond_to do |format|
      if @donation.save
        format.html { redirect_to donation_url(@donation), notice: "Donation was successfully created." }
        format.json { render :show, status: :created, location: @donation }
      else
        format.html { render :new, status: :unprocessable_entity }
        format.json { render json: @donation.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /donations/1 or /donations/1.json
  def update
    respond_to do |format|
      if @donation.update(donation_params)
        format.html { redirect_to donation_url(@donation), notice: "Donation was successfully updated." }
        format.json { render :show, status: :ok, location: @donation }
      else
        format.html { render :edit, status: :unprocessable_entity }
        format.json { render json: @donation.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /donations/1 or /donations/1.json
  def destroy
    @donation.destroy

    respond_to do |format|
      format.html { redirect_to donations_url, notice: "Donation was successfully destroyed." }
      format.json { head :no_content }
    end
  end

  private
    def set_donation
      @donation = Donation.find(params[:id])
    end

    def donation_params
      params.require(:donation).permit(:name, :email, :amount, :project_id)
    end

Upvotes: 0

Views: 351

Answers (1)

It worked here:

config/routes.rb

Rails.application.routes.draw do
    root "donations#new"
    resources :donations, only: [:new, :create]
end

app/views/donations/new.html.erb

<%= form_with model: @donation, local: true, method: :post do |form| %>
<div class="row__form-control">
<div class='row__form-control_amount'>
  <%= form.radio_button :amount, 25, class: "radio_button25", :onclick=>"notification()" %>
  € <%= form.label :amount, 25 %>
</div>

<div class='row__form-control_amount'>
  <%= form.radio_button :amount, 50, class: "radio_button50", :onclick=>"notification()" %>
  € <%= form.label :amount, 50  %>
</div>

<div class='row__form-control_amount'>
  <%= form.radio_button :amount, 75, class: "radio_button75", :onclick=>"notification()" %>
  € <%= form.label :amount, 75 %>
</div>

<div class='row__form-control_amount'>
  <%= form.radio_button :amount, 0, class: "radio_custom", :onclick=>"custom()" %> 
  <%= form.label :custom %>
</div>
<%= form.submit "Create Donation" %>

I created 2 records with 50 and 75 euros!

enter image description here

Upvotes: 0

Related Questions