1

_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.Picture1.jpg

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

Any suggestions are most welcome.

Thank you in advance.

14
  • This one should be easy to adapt ... stackoverflow.com/questions/31291645/… if you need more details you just goto the Jquery documentation page. Commented Sep 25, 2017 at 11:29
  • Per convo in chat - here's a question that demonstrates how to add fields to a form - the code in here goes inside the code that 'triggers' off the 'anchor' in the previous link I gave you ... stackoverflow.com/questions/6099301/… Commented Sep 25, 2017 at 13:28
  • Here's a helpful tutorial that will walk you through the theory (its wrong version but tells you how to go about the process). You will have to write your own condition to test. rubyplus.com/articles/… Commented Sep 25, 2017 at 14:00
  • What happens if you wrap <td class="col-1"><%= expense.description %>&nbsp in a conditional so it doesn't display if <%= link_to expense.description_other,{}, {:style => 'color: #CC3366'} %> is displayed? Commented Sep 27, 2017 at 14:45
  • 1
    I added up the controller, hope you have almost all the code needed here. Commented Oct 5, 2017 at 19:29

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

14 Comments

Thanks for the reply. Regarding this,<expression> ? <if true do this happens>: <if false this happens> I tried and I have , <%= (Expense.last.present? ? expense.description : expense.description_other) %> But one problem it shows only 'OTHERS' as the description.
I updated the /expenses/new in the controller and now it is working fine.
Awesome, if that works now, you could select the answer on the question & start a new one for further questions.
I go for this, <% if expense.description && expense.description_other %> <td class="col-1"><%= expense.description.sub('OTHERS','') %>&nbsp;<%= link_to expense.description_other,{}, {:style => 'color: #CC3366'} %></td> <% end %> and it works just after I used the .sub method.
You shouldn't have to do that, have you gone into rails console & done the @expense.description tests I talked about the other week? If it's not fix I posted here ... it's probably the serialization of the hash.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.