Rails - Simple Form - Money-Rails gem - show selection on edit

409 views Asked by At

I am trying to make an app in rails 4, using simple form and money-rails.

I have attributes in my model that are monetised.

When I create a new form and save attributes, I want those saved inputs to display when I come back and edit the form. At the moment, I'm trying:

<%= par.input :participation_cost,
              label: false,
              placeholder: 'Whole numbers only',
              selected: :participation_cost,
              :input_html => {
                :style => 'width: 250px; margin-top: 20px',
                class: 'response-project'
              } %>

I had hoped that 'selected: :participation_cost, would display the selected input. It doesn't. When you click edit to edit the form, you go back to the top of the list.

My participant controller has:

class ParticipantsController < ApplicationController
  before_action :set_participant, only: [:show, :edit, :update, :destroy]

  # GET /participants
  # GET /participants.json
  def index
    @participants = Participant.all
  end

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

  # GET /participants/new
  def new
    @participant = Participant.new
  end

  # GET /participants/1/edit
  def edit
  end

  # POST /participants
  # POST /participants.json
  def create
    @participant = Participant.new(participant_params)

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

  # PATCH/PUT /participants/1
  # PATCH/PUT /participants/1.json
  def update
    respond_to do |format|
      if @participant.update(participant_params)
        format.html { redirect_to @participant, notice: 'Participant was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @participant.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /participants/1
  # DELETE /participants/1.json
  def destroy
    @participant.destroy
    respond_to do |format|
      format.html { redirect_to participants_url }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def participant_params
      params[:participant].permit(:title, :description, :location, :costs, :participation_cost,
      :eligibility, :eligibility_criteria, :currency, :participation_cost_pennies, :participation_cost_currency,
      :location_specific )
    end
end
1

There are 1 answers

5
Andy Waite On

It shouldn't be necessary to set selected, simple_form should handle this. Try outputting <%= debug par %> in the edit view to ensure it has the value you expect.