0

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.

  • 3
    try `<% if params[:user][:options]? %>...<% else %>...<% end %>`. Currently you are missing an `<% end %>` for each of the `if`s and accessing the parameters incorrectly. The `?` is really just a shortcut for if it's true or false and the question mark symbol is associated in rails methods with booleans. – martincarlin87 Jun 19 '14 at 11:25
  • <%= params[:user][:options]? ? 'Status:Ma doktora' : 'Status:Nie ma doktora' %> – ReggieB Jun 19 '14 at 11:34
  • @ReggieB might be worthwhile explaining that ternary `if` format. – martincarlin87 Jun 19 '14 at 11:35
  • Hmm still not this, got error `F:/p15/app/views/pacjencis/show.html.erb:33: syntax error, unexpected ';' ...; if params[:user][:options]? ;@output_buffer.safe_concat('S... ... ^ F:/p15/app/views/pacjencis/show.html.erb:40: syntax error, unexpected keyword_ensure, expecting $end` – Achtung303c Jun 19 '14 at 11:38
  • @Achtung303c, to whose comment are you referring to, mine or ReggieB's? – martincarlin87 Jun 19 '14 at 11:47
  • The code should always be written in English. Otherwise you'll end up with nonsenses like `pacjencis`. It is possible to wirte it in polish, but you need to change default rails inflector, which is not easy. However even then, the model should be called `Pacjent`. On a separate note, your code doesn't look very raily. You shouldn't create inputs with pure html, there are helpers for that. Looking into your controller, you are not storing those inputs anyway and thay are not binded to any model, which doesn't make very much sense. I strongly recommend you to read some good rails book. – BroiSatse Jun 19 '14 at 11:48
  • Well, I got this part from simple_form https://github.com/plataformatec/simple_form#collection-radio-buttons – Achtung303c Jun 19 '14 at 12:00

3 Answers3

1
<% 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.

martincarlin87
  • 10,848
  • 24
  • 98
  • 145
0

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 %>
shankardevy
  • 4,290
  • 5
  • 32
  • 49
0
<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?

Community
  • 1
  • 1
ReggieB
  • 8,100
  • 3
  • 38
  • 46