Achtung303c
Achtung303c

Reputation: 23

Ruby on rails, radio button if

I'm a little green with ruby but wanted to try something with it, so I want to ask you for help. I have a problem with radio buttons. I have succesfully created a radio buttons, like this:

<input id="user_options_true" name="user[options]" type="radio" value="true" /> 
<label class="collection_radio_buttons" for="user_options_true">Tak</label>
<input id="user_options_false" name="user[options]" type="radio" value="false" />
<label class="collection_radio_buttons" for="user_options_false">Nie</label>

But now I would like to do if statement on the other page that woudl work like this: if value true, Write text, if false, write different text. I tried doing it like this :

 <% if user[options] == true %>
 <b>Status:Ma doktora </b>
 <% if user[options] == false %>
 <b>Status:Nie ma doktora</b>

But I get error like this:

F:/p15/app/views/pacjencis/show.html.erb:47: syntax error, unexpected keyword_ensure, expecting keyword_end
F:/p15/app/views/pacjencis/show.html.erb:49: syntax error, unexpected $end, expecting keyword_end

My view:

<%= simple_form_for(@pacjenci, html: {class: "form-horizontal"}) do |f| %>
  <%= f.error_notification %>

  <div class="form-inputs">
    <%= f.input :name %>
    <%= f.input :last_name %>
    <%= f.input :adres %>
    <%= f.input :pesel %>
    <%= f.input :telefon %>
    <%= f.input :email %>
    Czy lekarz przyjmował
    <input id="user_options_true" name="user[options]" type="radio" value="true" /> 
    <label class="collection_radio_buttons" for="user_options_true">Tak</label>
    <input id="user_options_false" name="user[options]" type="radio" value="false" />
    <label class="collection_radio_buttons" for="user_options_false">Nie</label>

  </div>

  <div class="form-actions">
    <%= f.button :submit %>
  </div>
<% end %>

My controler:

> class PacjencisController < ApplicationController
  # GET /pacjencis
  # GET /pacjencis.json
  def index
    @pacjencis = Pacjenci.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @pacjencis }
    end
  end

  # GET /pacjencis/1
  # GET /pacjencis/1.json
  def show
    @pacjenci = Pacjenci.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @pacjenci }
    end
  end

  # GET /pacjencis/new
  # GET /pacjencis/new.json
  def new
    @pacjenci = Pacjenci.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @pacjenci }
    end
  end

  # GET /pacjencis/1/edit
  def edit
    @pacjenci = Pacjenci.find(params[:id])
  end

  # POST /pacjencis
  # POST /pacjencis.json
  def create
    @pacjenci = Pacjenci.new(params[:pacjenci])

    respond_to do |format|
      if @pacjenci.save
        format.html { redirect_to @pacjenci, notice: 'Pacjenci was successfully created.' }
        format.json { render json: @pacjenci, status: :created, location: @pacjenci }
      else
        format.html { render action: "new" }
        format.json { render json: @pacjenci.errors, status: :unprocessable_entity }
      end
      end
      end

      # PUT /pacjencis/1
       # PUT /pacjencis/1.json
       def update
        @pacjenci = Pacjenci.find(params[:id])

      respond_to do |format|
      if @pacjenci.update_attributes(params[:pacjenci])
        format.html { redirect_to @pacjenci, notice: 'Pacjenci was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @pacjenci.errors, status: :unprocessable_entity }
        end
       end
       end

     # DELETE /pacjencis/1
     # DELETE /pacjencis/1.json
    def destroy
    @pacjenci = Pacjenci.find(params[:id])
    @pacjenci.destroy

    respond_to do |format|
      format.html { redirect_to pacjencis_url }
      format.json { head :no_content }
      end
     end
     end

Would really apreciate help.Thanks in advance.

Upvotes: 0

Views: 470

Answers (3)

martincarlin87
martincarlin87

Reputation: 11042

<% if params[:user][:options] == 'true' %>
  <b>Status:Ma doktora </b>
<% else %>
  <b>Status:Nie ma doktora</b>
<% end %>

I'd recommend using <strong> tags instead of <b>.

For true or false values you'd usually use <% if params[:user][:options]? %> but since this is an actual string value and not a boolean, the answer above should work.

The params part is how you access POST and GET data passed from your forms.

Upvotes: 1

ReggieB
ReggieB

Reputation: 8212

<b><%= params[:user][:options]? ? 'Status:Ma doktora' : 'Status:Nie ma doktora' %></b>

This solution uses a ternary operator: See How do I use the conditional operator (? :) in Ruby?

Upvotes: 0

shankardevy
shankardevy

Reputation: 4370

You need to add elsif and end like below:

<% if user[options] == true %>
 <b>Status:Ma doktora </b>
<% elsif user[options] == false %>
 <b>Status:Nie ma doktora</b>
<% end %>

Upvotes: 0

Related Questions