我有一个带有SalesOpportunities的应用程序 - 在这些下面有SaleQualifiers(belong_to SalesOpportunity
),它们分为4个阶段(" Need"," Budget",&#34 ; Fit","谈判")。我将这4个阶段显示为SalesOpportunity展示页面上的标签,当用户点击标签时,它将调用自定义控制器操作以选择要在下方窗格中显示的相关SaleQualifiers。到目前为止,这种方法效果很好,但我尝试确保所选阶段的标签更新为"活动"选项卡 - 这不起作用。窗格中包含正确的信息,但选项卡不正确:
SalesOpportunity显示控制器操作:
def show
@sales_opportunity = SalesOpportunity.find(params[:id])
session[:sales_opportunity_id] = @sales_opportunity.id
#set the SaleQualifier stages to show on the tabs
@stages = Question.order(:id).pluck(:sale_stage).uniq
@stage = @sales_opportunity.sale_status
@questions = Question.where(sale_stage: @stage)
#find any answered sale_qualifiers in this sale_stage
@sale_qualifiers = SaleQualifier.find_by_sql(["SELECT sale_qualifiers.* FROM sale_qualifiers INNER JOIN questions ON questions.id = sale_qualifiers.question_id WHERE sales_opportunity_id = ? AND questions.sale_stage = ? ", @sales_opportunity.id, @stage])
#find some unanswered sale_qualifiers and build them
@sale_qualifier = SaleQualifier.new(sales_opportunity_id: params[@sales_opportunity.id])
@answer = @sale_qualifier.build_answer
#find the right question to render and link to the sale_qualifier
@question = Question.find(@sales_opportunity.next_question)
end
此操作仅确保在首次加载SalesOpportunity显示页面时设置右窗格。
以下是根据自己的行为找到合适的SaleQualifiers的自定义操作:
def prospect_qualifiers
@sales_opportunity = SalesOpportunity.find_by(id: session[:sales_opportunity_id])
@stage = params[:sale_stage]
@questions = Question.where(sale_stage: @stage)
@stages = Question.order(:id).pluck(:sale_stage).uniq
@sale_qualifiers = SaleQualifier.find_by_sql(["SELECT sale_qualifiers.* FROM sale_qualifiers INNER JOIN questions ON questions.id = sale_qualifiers.question_id WHERE has_answer = 'true' AND sales_opportunity_id = ? AND questions.sale_stage = ? ", @sales_opportunity.id, @stage])
#when there's no sale_qualifiers from this sale_stage we can just select the first one according to the question and build that
if @sale_qualifiers.blank?
@question = Question.order(:id).where(sale_stage: @stage).first
@sale_qualifier = SaleQualifier.new(sales_opportunity_id: params[@sales_opportunity.id], question_id: @question.id)
@answer = @sale_qualifier.build_answer
render :modal_form
else
#when some of the sale_qualifiers of this sale_stage exist we'll need to be more specific about which question is next
@sale_qualifier = SaleQualifier.new(sales_opportunity_id: params[@sales_opportunity.id])
@answer = @sale_qualifier.build_answer
#find the right question to render and link to the sale_qualifier
@question = Question.find(@sales_opportunity.next_question)
render :modal_form
end
end
这会将sale_stage
作为从链接传递给此操作的参数,并使用它来查找正确的SaleQualifiers。
我的观看代码的违规部分如下:
<div id="qualifier-tabs">
<ul class="nav nav-tabs">
<%= render partial: 'shared/tabs', collection: @stages, as: :stage %>
</ul>
</div>
<div class="tab-content", id="qualifier_panel">
<%= render partial: 'shared/panel', collection: @stages, as: :stage %>
</div>
</div>
shared / tabs partial:
<% if stage == @stage %>
<li class="active"><%= link_to stage, prospect_qualifiers_sale_qualifiers_path(:sale_stage => stage), :remote => true, data: { disable_with: "Loading...", toggle: 'tab' } %></li>
<% else %>
<li><%= link_to stage, prospect_qualifiers_sale_qualifiers_path(:sale_stage => stage), :remote => true, data: { disable_with: "Loading...", toggle: 'tab' } %></li>
<% end %>
正如您所看到的,这会将collection: @stages as: stage
与从控制器操作传递的@stage变量进行比较,确定它们是否匹配 - 如果是,我们将其设置为活动选项卡。
shared/panel
部分:
<% if stage == @stage %>
<div class="tab-pane active" id='<%=stage %>'>
<table class="table table-striped display responsive no-wrap">
<thead>
<tr>
<th>Question</th>
<th>Answer</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<!-- if a sale_qualifier exists with an answer show the following -->
<% @sale_qualifiers.each do |qual| %>
<tr>
<td><%= qual.question.question_text %></td>
<% if qual.question.answer_type == "Datetime"%>
<td><%= qual.answer.answer_text.to_date.readable_inspect %></td>
<% elsif qual.question.answer_type == "Boolean" %>
<% if qual.answer.answer_text == 'true'%>
<td>Yes</td>
<% elsif qual.answer.answer_text == 'false' %>
<td>No</td>
<% end %>
<% else %>
<td><%= qual.answer.answer_text %></td>
<% end %>
<td><%= link_to('edit', edit_sale_qualifier_path(qual), class: "btn btn-sm btn-warning", data: { disable_with: "Loading...", dismiss: 'modal' }, :remote => true )%></td>
</tr>
<% end %>
<!-- if there's no sale_qualifier with the question id then build one -->
<%= form_tag('/sale_qualifiers', :id => 'new_sale_qualifier', :class => 'form', method: :post, remote: true, data: { model: "sale_qualifier" }) do -%>
<%= fields_for :sale_qualifier do |ff| %>
<%= ff.hidden_field :sales_opportunity_id, :value => @sales_opportunity.id %>
<%= ff.hidden_field :question_id, :value => @question.id %>
<tr>
<td><%= @question.question_text %></td>
<td>
<%= ff.fields_for :answer_attributes do |answer| %>
<div class="form-group">
<% if @question.answer_type == 'Text Field' %>
<%= answer.text_area :answer_text, :placeholder => "Enter your answer"%>
<% end %>
<% if @question.answer_type == 'Datetime' %>
<div class='input-group date' id='datetimepicker' data-date-format="YY.MM.DD">
<%= answer.text_field :answer_text, class: "form-control", data: { date_format: 'YYYY/MM/DD' }, :placeholder => "YYYY/MM/DD" %>
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
<% end %>
<% if @question.answer_type == 'Boolean' %>
<%= answer.select :answer_text, [['Yes', true], ['No', false]] %>
<% end %>
<span class="warning-block"></span>
<span class="help-block"></span>
</div>
<% end %>
</td>
<td>
<%= submit_tag "Submit", class: "btn btn-large btn-success", id: "sale_qualifier_submit", data: { disable_with: "Submitting..." }, autocomplete: 'off' %>
</td>
</tr>
<% end %>
<% end %>
</tbody>
</table>
</div>
<% else %>
<div class="tab-pane" id='<%=stage %>'>
</div>
<% end %>
窗格执行相同的等效性测试,并决定如何显示SaleQualifiers。
这里是modal_form
代码:
$('#qualifier_tabs').html("<%= j render :partial => 'shared/tabs', collection: @stages, as: :stage %>");
$('#qualifier_panel').html("<%= j render :partial => 'shared/panel', collection: @stages, as: :stage %>");
问题是该窗格显示正确的SaleQualifiers,但活动标签仍然显示在&#34; Need&#34;。如果我将<%= @stage %>
和<%= stage %>
输出放入标签部分,也放入面板部分我得到原因 - 标签面板始终显示&#34;需要&#34; &#34;需要&#34;作为那些erb片段的输出,而窗格面板显示实际来自sale_stage
动作的prospect_qualifiers
。
prospect_qualifiers
操作(调用modal_form
文件并因此将两个部分都放入视图中)如何将@stage
和stage
的不同值传递给这两个当它们被同一个控制器动作渲染时会出现偏差吗?
修改 - 添加节目视图
Per Chloe的请求 - 这是SalesOpportunity展示页面的展示视图(或相关部分):
<div class="panel panel-default">
<div id="qualifier-tabs">
<ul class="nav nav-tabs">
<%= render partial: 'shared/tabs', collection: @stages, as: :stage %>
</ul>
</div>
<div class="tab-content", id="qualifier_panel">
<%= render partial: 'shared/panel', collection: @stages, as: :stage %>
</div>
</div>
答案 0 :(得分:0)
我现在看到你的视图中有<div id="qualifier-tabs">
,但是你的JQuery中有$('#qualifier_tabs')
。