从下拉列表中选择"OTHERS"选项,创建文本字段Ruby on Rails-4.2 [英] Select 'OTHERS' option from drop down list creating text field Ruby on Rails - 4.2

查看:77
本文介绍了从下拉列表中选择"OTHERS"选项,创建文本字段Ruby on Rails-4.2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

_form.html.erb

_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

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

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

expense.js

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

general.scss

general.scss

#otherDesc {
display:none;
}

一切正常,除了我的索引页面外,在该页面上我获得了"OTHERS"选定选项的两个值,即"OTHERS + MY OWN description".例如,在图像中为OTHERS WHITE GLUE.但是我只想用白胶做为描述.

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.

我已经尽力了,但是无法获得想要的结果.

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

任何建议都是最欢迎的.

Any suggestions are most welcome.

谢谢.

推荐答案

找到了-有时候我有点盲目...

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

然后

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

选择了OTHER后,使用if条件语句(我们讨论过的<expression> ? <if true do this happens>: <if false this happens>三元运算符,条件语句必须在一个<%= ... %>块中包含两个语句)来获取第一个语句.

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.

此外,您在这里遇到问题...应该有某种条件...可能是||=&放下Expense.new或Expense.last.dup

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

这篇关于从下拉列表中选择"OTHERS"选项,创建文本字段Ruby on Rails-4.2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆