2014-06-19 90 views
0

我有點紅寶石般的綠色,但想嘗試一下它,所以我想問你的幫助。我對單選按鈕有問題。我已成功創建了一個單選按鈕,如下所示:紅寶石軌,單選按鈕如果

<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> 

但現在我想這樣做,如果其他網頁上聲明woudl工作是這樣的:如果值爲true,輸入文字,如有虛假,編寫不同的文本。我試圖做這樣的:

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

,但我得到的錯誤是這樣的:

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 

我的觀點:

<%= 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 %> 

我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 

真的會apreciate help.Thanks提前。

+3

嘗試'<%if params [:user] [:options]? %> ... <% else %> ... <% end %>'。目前,您錯過了每個'if'並錯誤地訪問參數的'<% end %>'。 '?'實際上只是一個捷徑,如果它是真或假,並且問號符號與rails方法與布爾值相關聯。 – martincarlin87

+0

<%= params [:user] [:options]? ? '狀態:Ma doktora':'狀態:Nie ma doktora'%> ReggieB

+0

@ReggieB可能值得解釋三元'if'格式。 – martincarlin87

回答

0

您需要添加elsifend象下面這樣:

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

遇到錯誤liek爲#這個 '未定義局部變量或方法'用戶<#<類別:0x59ae268> :0x57df0c8>' – Achtung303c

+0

只是意味着,你有沒有所謂的「用戶」局部變量。我需要查看控制器和視圖的代碼以進一步調試。 – shankardevy

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

我推薦使用<strong>標籤,而不是<b>

對於你通常會使用<% if params[:user][:options]? %>但由於這是一個實際的字符串值,而不是一個布爾值,回答上述應該工作truefalse值。

params部分是您如何訪問POSTGET從表單傳遞的數據。