_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 %>
experience_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
ensem.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。但我只想以白胶为例。
请找到随附的图像以供参考。
我已经尽力了,但无法获得预期的结果。
任何建议都是最欢迎的。
先感谢您。
最佳答案
找到了-有时候我有点盲目...
index.html.erb
<td class="col-1"><%= expense.description %> <%= 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>
的三进制运算符,条件语句必须在一个<%= ... %>
块中包含两个语句)来获取第一个语句。另外,您在这里遇到问题...应该具有某种条件...可能是
||=
并删除Expense.new或Expense.last.dup def new
@expense = Expense.new
@expense = Expense.last.dup
end
关于javascript - 从创建文本字段Ruby on Rails-4.2的下拉列表中选择“其他”选项。,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46399104/