我有3个模型,员工有很多保险,员工有很多教育,保险和教育都属于员工。
整个流程运行正常。
现在我有一个要求,当我点击员工时 - >编辑,我必须在编辑表格下显示保险和教育链接的侧边栏。当我点击保险/教育时,它应该在同一个编辑页面中呈现。
员工/ edit.html.erb
<h1>
<%= @employee.full_name %>
</h1>
<div class="tabpanel">
<div class="tab-content">
<div class="tab-pane active" id="home" role="tabpanel">
<%= render 'form' %>
</div>
</div>
</div>
<%= render 'layouts/sidebar' %>
布局/ _sidebar.html.erb
<div class="sidebar">
<ul class="nav">
<li>
<%= link_to image_tag("Insurances.png")+"Insurances", employee_insurances_path(@employee) %>
</li>
<li>
<%= link_to image_tag("Education.png")+"Education", employee_educations_path(@employee) %>
</li>
</ul>
</div>
我的保险控制员
class InsurancesController < ApplicationController
before_action :set_insurance, only: [:show, :edit, :update, :destroy]
respond_to :html
def index
@employee = Employee.find(params[:employee_id])
@insurances = @employee.insurances.all
respond_with(@insurances)
end
def show
@employee = Employee.find(params[:employee_id])
respond_with(@insurance)
end
def new
@employee = Employee.find(params[:employee_id])
@insurance = @employee.insurances.build
end
def edit
@employee = Employee.find(params[:employee_id])
end
def create
@employee = Employee.find(params[:employee_id])
@insurance = @employee.insurances.create(insurance_params)
redirect_to employee_path(@employee)
end
def update
@employee = Employee.find(params[:employee_id])
@insurance.update(insurance_params)
redirect_to employee_path(@employee)
end
def destroy
@employee = Employee.find(params[:employee_id])
@insurance.destroy
redirect_to employee_path(@employee)
end
private
def set_insurance
@insurance = Insurance.find(params[:id])
end
def insurance_params
params.require(:insurance).permit(:name_of_dependent, :relationship,:policy_numbere)
end
end
当我点击侧边栏链接时,知道如何渲染同一页面吗?
答案 0 :(得分:0)
我的理解是你有两个编辑表格,一个用于保险,另一个用于教育。或者你可以有一个表格。您需要做的是当用户点击侧栏中的链接时向控制器发送AJAX请求。创建表单的一部分。并且在AJAX请求成功时,可以在页面上随意添加表单,如下所示:
$('#container').append('<%= escape_javascript(render :partial => "form", :locals => {:employee => employee}) %>');
本地人是可选的。 #container
是一个section / div或现有页面的DOM中的任何元素,您可以将其重命名为任何内容。并且在您发送请求的操作中的控制器中,您需要这样写:
respond_to do |format|
format.js
end
这将搜索你的行动名称的文件,扩展名为js.eb.例如,您将其发送到edit
操作,那么视图文件夹中的文件名应为edit.js.erb
。并在js.erb中编写附加部分的代码。
希望这有帮助。