我的要求:在同一页面上列出并编辑表单
每当用户点击“编辑”按钮时,将显示表单编辑(默认隐藏),表单中的数据将填充被点击的行中的数据。用户编辑名称后单击“保存”按钮 - >行表格将在没有刷新页面的情况下更新。
文件index.html.haml
%label.lblCapTbl Client Organizations
.edit_client_org
#ShowHideForm
%strong Edit Client Organization
.client_orgs_edit_from
= render :partial => 'edit'
.wrapper-table
%table#tbl-orgs.table.table-striped
%thead
%tr
%th Client Organizations
%th Client Account Directors
%th
%th
%tbody
= render @client_orgs
档案_edit.html.haml
#form_edit_client_org.formEditShow
= form_for(@client_org,remote: true) do |f|
#error_explanation
.form_edit_org
=f.label :"Name of Client Organization"
=f.text_field :name ,:autofocus => true ,class: "input_name",placeholder: "Name"
%br
=f.label :"Client Account Director(email address)"
%ul
.btn_form_add
= button_tag "Cancel", type: :button, id: "cancelForm", class: "btn"
= f.submit "Save", :name => 'done',class:"btn"
文件_client_org.html.haml
%tr{:id => "client_org_#{client_org.id}"}
%td= client_org.name
%td
= client_org.users.first.email
%td
= link_to "Edit", edit_org_path(client_org), :remote => true,class: 'btn link_edit_client_org'
当用户按下“编辑”按钮时。文件ClientOrgController
def edit
@client_org = ClientOrg.find(params[:id])
respond_to do |format|
format.json { render json: @client_org, status: :created }
end
end
def update
//TODO
end
ajax
$('.link_edit_client_org').on('ajax:success', function(evt, data, status, xhr){
$(".edit_client_org").show();
$('input[name="client_org[name]"]').val(data.name);
});
我的问题
当我按下编辑表单上的“保存”按钮时 - >在ClientOrgController中,ajax调用create action而不是update action?。我发现在表单中编辑@client_org变量总是为nil,因为在编辑函数中(在ClientOrgControler中)我返回需要填写编辑表单的json数据。
任何人都可以帮助我吗?提前致谢