提交按钮的简单表单不做任何事情

时间:2015-10-23 21:27:54

标签: ruby-on-rails ruby

我在下面有这个代码加载患者数据,每个更新按钮更新它,但点击没有任何反应,这里是代码:

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

以下是发送表单的paitent控制器,用于对发送的paitent进行更新:

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

2 个答案:

答案 0 :(得分:1)

这里有几个问题需要解决:

<% 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. 始终使用snake_case作为属性(IDNumber是baaad umkay)
  2. 查看state_select gem
  3. 循环是保持表格简洁的最佳方式。高效
  4. 您的表单正在发送到edit操作 - 您需要发送到update操作
  5. #4将回答您的问题 - patients_edit_path(patient.id)

    您需要的是发送到update路径:patient_path(patient), method: :put ...或只是:patient, method: :put

答案 1 :(得分:0)

让简单的表单在url,方法等上为你工作,除非你有自定义的东西。如果这不起作用,请发布有关您在帖子中收到的错误的更多信息。

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