2015-10-23 53 views
1

下面我有此代碼加載患者數據和與它,但是,點擊沒有反應每一個更新按鈕更新,這裏是代碼:簡單的表單提交按鈕不會做任何事情

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

回答

0

讓簡單的形式做的工作給你的網址,方法等,除非你有東西是定製。如果這不起作用,請發佈有關您在帖子中收到的錯誤的更多信息。

<% emergency_case.patients.each do |patient| %> 
    <tr> 
    <%= simple_form_for patient do |f|%> 
     ....#form stuff 
     <td><%=f.submit %></td> 
    <% end %> 
    </tr> 
<% end %> 
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寶石
  3. 循環是保持表格簡潔的最佳方式&高效
  4. 您的形式發送到edit行動 - 你需要發送到update行動

#4將會回答你的問題 - patients_edit_path(patient.id)

你需要的是送到update路徑:patient_path(patient), method: :put ...或簡單地:patient, method: :put