使用select_tag传入整数

时间:2016-10-27 11:49:07

标签: ruby-on-rails

所以这是我的控制器

<body data-ng-app="myApp">

<div data-ng-controller="alfaCtrl1" class="blogwrapper" id="blogFirst">
<span>{{date}}</span><span></span>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http) {
    $http.get('jsonfile.json').success(function(data) {
        $scope.date = data.posts[0].date;
    });
});
</script>
</body>

这是模型中的方法

def index @tutor = Tutor.where(:admin => false) @tutor = @tutor.subject_search(params[:subject_search]) if params[:subject_search].present? @tutor = @tutor.fees_search(params[:fees_search]) if params[:fees_search].present? end 方法

fees_search

这是def self.fees_search(amount) @tutor ||= Tutor.where(admin: false) @tutor.map do |tutor| @fees = tutor.profile.fees if @fees <= amount #puts 'if is working' @tutor = tutor else #puts 'else is working' @tutor = nil end @tutor end end 方法

subject_search

所以我可以在我的rails控制台中执行类似def self.subject_search(name) @result = Subject.where("name LIKE ?" , "#{name}").take @tutor = @result.tutors end 的操作,然后是@tutor = Tutor.subject_search('English'),我会得到一个结果。

但是,在索引视图中完成后,我收到错误@tutor.fees_search(20)

所以基于表格中的观点

comparison of Fixnum with String failed

当我在浏览器中检查时,我看到<%= label_tag 'fees_search', 'Fees' %> <%= select_tag 'fees_search', options_from_collection_for_select(, :selected => params[:fees_search]), :include_blank => true, class:'form-control' %> <%= submit_tag 'Filter', class: 'btn btn-primary btn-xs' %> (依此类推),我猜是错误出现的原因?如果是这样的话,我怎样才能将<option value = "10">设置为输出select_tag值呢?

或者是否有更优雅的主题和/或费用过滤解决方案?我是否需要发布关系?

1 个答案:

答案 0 :(得分:1)

参数值始终是您需要使用to_i将参数值转换为整数的字符串

@tutor = 
  @tutor.fees_search(params[:fees_search].to_i) if params[:fees_search].present?

或者您可以在方法本身中将其转换为整数。

if @fees <= amount.to_i # This is where actually you are getting the error
  @tutor = tutor
else
  @tutor = nil
end

这将为您提供预期的结果。