简体   繁体   中英

Select 'OTHERS' option from drop down list creating text field Ruby on Rails - 4.2

_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;
}

Everything works fine except my index page where I get two values for 'OTHERS' selected option as OTHERS + MY OWN DESCRIPTION. For example in the image it is OTHERS WHITE GLUE. But I would like to have only WHITE GLUE as the description.

Please find attached the image for your reference. 图片1.jpg

I have tried too hard but unable to get the desired result.

Any suggestions are most welcome.

Thank you in advance.

Found it - I'm kind of blind sometimes...

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

You've got two <%= ... %> 's in there ...

<%= expense.description %>

And then

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

Get ride of the first one when OTHER is selected, using an if conditional(the trinary operator we talked about <expression> ? <if true do this happens>: <if false this happens> The conditional would have to contain both statements inside one <%= ... %> block.

Also, you have an issue here ... should have a conditional of some sort ... probably the ||= & drop the Expense.new or the Expense.last.dup

  def new
    @expense = Expense.new

    @expense =  Expense.last.dup
  end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM