我有一个使用 Bootstrap 选项卡的 Rails 应用程序。每个选项卡显示一个从部分呈现的数据表。数据表使用文件中定义的 ajax 代码:workorders3_datatable.rb 和 workorders4_datatable.rb。
这是索引代码:
<div class="tab-pane" id="tab2">
<%= render :partial => "workorders/index7", locals: {workorders: @workorders, tab: 2} %>
</div>
<div class="tab-pane" id="tab3">
<%= render :partial => "workorders/index8", locals: {workorders: @workorders, tab: 3} %>
这是我试图使工作的控制器代码:
def index
@workorders = Workorder.all
@tab = :tab
respond_to do |format|
format.html # index.html.erb
if @tab == 2
format.json { render json: Workorders3Datatable.new(view_context) }
else
format.json { render json: Workorders4Datatable.new(view_context) }
end
end
结尾
我确信这个if
说法是错误的。
谢谢您的帮助。
更新1
(这有点难以解释)
但是,我需要 json 来自Workorders3Datatable
tab2 和Workorders4Datatable
tab3 的文件。
那么,我该如何编写一个if
stmt 来测试 tab2 与 tab3 的区别?
更新2
我将视图代码更改为此 - 但是,它仍然Workorders4Datatable
用于两个选项卡视图。
<div class="tab-pane" id="tab2">
<%= render :partial => "workorders/index7", :locals => {:workorders => @workorders, :tab_id => 2} %>
</div>
<div class="tab-pane" id="tab3">
<%= render :partial => "workorders/index8", :locals => {:workorders => @workorders, :tab_id => 3} %>
<div>
控制器:
respond_to do |format|
format.html # index.html.erb
puts "PUTS +++++++++++++ tab_id:" + params[:tab_id].to_s
if params[:tab_id] == 2
format.json { render json: Workorders3Datatable.new(view_context) }
else
format.json { render json: Workorders4Datatable.new(view_context) }
end
end
该puts
语句显示 tab_id 为 nil THE CONTROLLER ISN'T GETTING THE PARAM
更新3
我正在尝试不同的东西 - 在控制器中有单独的操作
查看代码:
<div class="tab-pane" id="tab2">
<%= render :partial => "workorders/index7", :action => :index2 %>
</div>
<div class="tab-pane" id="tab3">
<%= render :partial => "workorders/index8", :action => :index3 %>
</div>
控制器(我必须离开索引,否则它不会显示)
def index
puts "PUTS +++++++++++ index"
respond_to do |format|
format.html # index.html.erb
format.json { render json: Workorders3Datatable.new(view_context) }
end
end
def index2
puts "PUTS +++++++++++ index2"
respond_to do |format|
format.html # index.html.erb
format.json { render json: Workorders3Datatable.new(view_context) }
end
end
def index3
puts "PUTS +++++++++++ index3"
respond_to do |format|
format.html # index.html.erb
format.json { render json: Workorders4Datatable.new(view_context) }
end
end
它最终一直在使用index
。
PS - 也试过
<%= render :partial => "workorders/index7", :action => "index2" %>