无法在Rails中从控制器呈现页面到视图



我得到404这个页面缺失。我需要将控制器结果传递给视图。我已经添加了各自的控制器和视图。我在控制器中获取值,但没有将这些值传递给视图。我也没有得到任何错误。有人能帮忙吗?

在代码下面我正在尝试

控制器

class CompareResultsController < ApplicationController
def compare_results
executionId1=params['executionId1']
executionId2=params['executionId2']
@execution_results1 = ExecutionResult.where(:execution_id => executionId1).pluck(:parametersname,:actual_value)

@execution_results2 = ExecutionResult.where(:execution_id => executionId2).pluck(:parametersname,:actual_value)

puts @execution_results1

puts @execution_results2


end

end

JS代码
function compareExecutionResults() {

var checkBoxValue=[]
$("input[type='checkbox']:checked").each(function(){
checkBoxValue.push($(this).val());
});
var executionId1=checkBoxValue[0];
var executionId2=checkBoxValue[1];

$.ajax({
type: "POST",
contentType: "application/json",
url: "/compare_results/compare_results",
data: JSON.stringify({
executionId1: executionId1,
executionId2: executionId2
}),
success: function (result) {
if (result === 'false') {
alert('Description  not Updated  !!!');
return false;
} else {
alert('Comparing Values !!!');
window.location.href = "/compare_results/compare_results";
}
}
}); 
}

路线

post "/compare_results/compare_results", to: "compare_results#compare_results"
HTML:

<table>
<tr>
<th>Sr.no</th>
<th>Param Name</th>
<th>Value</th>
<th>Param Name</th>
<th>Value</th>
<th>Matched</th>
</tr>
<% @execution_results.each do |result, index| %>
<tr>
<td><%= index + 1 %></td>
<td><%= result.name %></td>
<td><%= result.value %></td>
<td><%= @execution_results2[index].name %></td>
<td><%= @execution_results2[index].value %></td>
<td><%= compare_function(result, @execution_results2[index]) %></td>
</tr>
<% end %>
</table>

Html视图
Views--compare_results--compare_results.html.erb file

您需要确保控制器的compare_results方法在config/routes.rb文件中公开,因为它不是标准资源。

像这样的东西可能会有帮助:

post '/compare_results/compare_results', to: 'compare_results#compare_results'

很可能你没有给出正确的路径,检查这是否是问题,在终端运行以下命令,如果你的rails版本大于5则rails routes,或者如果它小于5则rake routes

通过这样做,你将能够看到你的应用程序正在服务的URL。现在检查是否有一个URL(路径)与控制器动作匹配。然后确保你使用了正确的路径。

注意:我所说的路径是指你在link_to中给出的访问本页的路径。

最新更新