从下拉列表中选择“OTHERS”选项,创建文本字段Ruby on Rails - 4.2

时间:2017-09-25 06:47:32

标签: javascript jquery ruby-on-rails-4

_form.html.erb

 <div class="form-group">
  <%= f.label :description %><br>
  <%= f.select(:description, options_for_select([['', ''],['METRO', 'METRO'], ['BUS', 'BUS'], ['TAXI', 'TAXI'], ['OTHERS', 'OTHERS']]), {}, {class: "form-control", id: "expense_description"}) %>
  <br>
  <div id="otherDesc">
    <%= f.text_field :description_other, class: "form-control" %>
  </div>
</div>

index.html.erb

<% @expenses.each do |expense| %>

<tr class="tr-<%= cycle('odd', 'even') %>">

<td class="col-1"><%= (expense.description_other.present? ? expense.description_other : expense.description) %></td>

</tr>

<% end %>

expenses_controller.rb

class ExpensesController < ApplicationController
  before_action :set_expense, only: [:show, :edit, :update, :destroy]

  # GET /expenses
  # GET /expenses.json
  def index
    @expenses = Expense.all
  end

  # GET /expenses/1
  # GET /expenses/1.json
  def show
  end

  # GET /expenses/new

   def new
    if Expense.last.present?
      @expense = Expense.last.dup
    else
      @expense = Expense.new
    end
   end

  # GET /expenses/1/edit
  def edit
  end

  # POST /expenses
  # POST /expenses.json
  def create
    @expense = Expense.new(expense_params)

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

  # PATCH/PUT /expenses/1
  # PATCH/PUT /expenses/1.json
  def update
    respond_to do |format|
      if @expense.update(expense_params)
        format.html { redirect_to @expense, notice: 'Expense was successfully updated.' }
        format.json { render :show, status: :ok, location: @expense }
      else
        format.html { render :edit }
        format.json { render json: @expense.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /expenses/1
  # DELETE /expenses/1.json
  def destroy
    @expense.destroy
    respond_to do |format|
      format.html { redirect_to expenses_url, notice: 'Expense was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_expense
      @expense = Expense.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def expense_params
      params.require(:expense).permit(:description, :description_other)
    end
end

expense.js

$(document).ready(function () {
      $('#expense_description').on('change',function(){
        var selectedValue = $(this).val();
        selectedValue == "OTHERS" ? $("#otherDesc").show() : $("#otherDesc").hide()
      });
 });

general.scss

#otherDesc {
display:none;
}

一切正常,除了我的索引页面,我得到两个'OTHERS'选择选项的值为OTHERS + MY OWN DESCRIPTION。例如,在图像中它是OTHERS WHITE GLUE。但是我想只有白色的GLUE作为描述。

请附上图片供您参考。Picture1.jpg

我试过太努力但无法获得理想的结果。

欢迎任何建议。

提前谢谢。

1 个答案:

答案 0 :(得分:1)

找到它 - 有时候我有点失明......

index.html.erb
<td class="col-1"><%= expense.description %>&nbsp;<%= link_to expense.description_other,{}, {:style => 'color: #CC3366'} %></td>

你在那里有两个<%= ... %> ......

<%= expense.description %>

然后

<%= link_to expense.description_other,{}, {:style => 'color: #CC3366'} %>

在选择OTHER时使用if条件(我们谈到的三元运算符<expression> ? <if true do this happens>: <if false this happens>来获取第一个条件。条件必须包含一个<%= ... %>块内的两个语句

此外,你在这里有一个问题......应该有某种条件......可能是||=&amp;删除Expense.new或Expense.last.dup

  def new
    @expense = Expense.new

    @expense =  Expense.last.dup
  end