mohamed
mohamed

Reputation: 135

Simple form for submit button doesn't do anything

I have this code below which loads the patient data and with each one the update button updates it but on clicking nothing happens, here is the code:

 <% emergency_case.patients.each do |patient| %>
              <tr>

               <%= simple_form_for (:patient),url: patients_edit_path(patient.id) do |f|%>
               <td><%=f.input :name ,:input_html => { :value => patient.name},label: false %></td>
               <td><%=f.input :IDNumber ,:input_html => { :value => patient.IDNumber},label: false %></td>
               <td><%=f.input :age ,:input_html => { :value => patient.age},label: false %></td>
               <td><%=f.input :phone ,:input_html => { :value => patient.phone},label: false %></td>
               <td><%=f.input :address ,:input_html => { :value => patient.address},label: false %></td>
               <td><%=f.input :injury ,:input_html => { :value => patient.injury},label: false %></td>
               <td><%= f.collection_select(:state_id, State.all, :id, :state) %></td>
               <td><%= f.collection_select(:Act, Act.all, :id, :act) %></td>
                <td><%=f.submit %></td>
                <% end %>
              </tr>
          <% end %>

Here is the paitent controller which am sending the form for to make updates on the paitent that is sent:

class PatientsController < ApplicationController
  before_action :set_patient, only: [:show, :edit, :update, :destroy]

  # GET /patients
  # GET /patients.json
  def index
    @patients = Patient.all
  end

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

  # GET /patients/new
  def new
    @patient = Patient.new
  end

  # GET /patients/1/edit
  def edit
    @patient =Patient.find(params[:id])
  end

  # POST /patients
  # POST /patients.json
  def create
    @patient = Patient.new(patient_params)

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

  # PATCH/PUT /patients/1
  # PATCH/PUT /patients/1.json
  def update
    respond_to do |format|
      if @patient.update(patient_params)
        format.html { redirect_to @patient, notice: 'Patient was successfully updated.' }
        format.json { render :show, status: :ok, location: @patient }
      else
        format.html { render :edit }
        format.json { render json: @patient.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /patients/1
  # DELETE /patients/1.json
  def destroy
    @patient.destroy
    respond_to do |format|
      format.html { redirect_to patients_url, notice: 'Patient was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def patient_params
      params.require(:patient).permit(:name, :isDead, :status, :IDNumber, :emergency_case_id,:state_id,:address,:age,:phone,:injury,:act)
    end
end

Upvotes: 1

Views: 1168

Answers (2)

Richard Peck
Richard Peck

Reputation: 76774

Several issues to contend with here:

<% emergency_case.patients.each do |patient| %>
   <%= content_tag :tr do %>
         <%= simple_form_for patient, method: :put do |f|%>
               <% attributes = %i(name IDNumber age phone address injury) %>
               <% patient.attributes do |attr| %>
                  <%= content_tag :td, f.input attr, input_html: { value: patient.send(attr)}, label: false %>
               <% end %>
               <%= content_tag :td, f.state_select :state_id %>
               <%= content_tag :td, f.collection_select(:Act, Act.all, :id, :act) %>
               <%= content_tag :td, f.submit %>
        <% end %>
    <% end %>
<% end %>

  1. ALWAYS use snake_case for attributes (IDNumber is baaad umkay)
  2. Check out the state_select gem
  3. Loops are the BEST way to keep forms succinct & efficient
  4. Your form is sending to the edit action -- you need to send to the update action

#4 will answer your question -- patients_edit_path(patient.id)

What you need is to send to the update path: patient_path(patient), method: :put... or simply: patient, method: :put

Upvotes: 1

Austio
Austio

Reputation: 6095

let simple form do the work for you on the url, method, etc unless you have something that is custom. If this doesn't work please post more info on the error you are getting in your post.

<% emergency_case.patients.each do |patient| %>
  <tr>
    <%= simple_form_for patient do |f|%>
       ....#form stuff
       <td><%=f.submit %></td>
    <% end %>
  </tr>
<% end %>

Upvotes: 0

Related Questions