Ruby on rails Rails搜索-传递参数,不传递和#x27;t返回结果(错误)
我正在尝试在我的Rails应用程序上使用简单搜索。我只有两个选择器可供选择:(a)种类和(b)位置id。表单显示,值可选,参数在url中传递。然而,当我提交时,即使有匹配的内容,也不会显示任何内容。我签入了我的活动管理员,那里有匹配搜索参数的地方 这是我的places\u controller.rbRuby on rails Rails搜索-传递参数,不传递和#x27;t返回结果(错误),ruby-on-rails,ruby,search,query-parameters,Ruby On Rails,Ruby,Search,Query Parameters,我正在尝试在我的Rails应用程序上使用简单搜索。我只有两个选择器可供选择:(a)种类和(b)位置id。表单显示,值可选,参数在url中传递。然而,当我提交时,即使有匹配的内容,也不会显示任何内容。我签入了我的活动管理员,那里有匹配搜索参数的地方 这是我的places\u controller.rb def index @places = Place.search(params[:kind],[:location_id]) end def self.search(kind, locati
def index
@places = Place.search(params[:kind],[:location_id])
end
def self.search(kind, location_id)
return scoped unless kind.present? || location_id.present?
where(['kind = ? AND location_id = ?', kind, location_id])
end
<%= form_tag places_path, :method => 'get' do %>
<form class="form-inline text-center" role="form" action="/places">
<div class="form-group">
<%= label_tag :kind %>
<%= select_tag :kind, options_for_select([['beer','0'],['chocolate','1'],['cocktail','2'], ['coffee','3'], ['tea','4'], ['wine','5'], ['juice','6']]), class: "form-control" %>
/div>
<br>
<div class="form-group">
<%= label_tag :location_id %><br>
<%= select_tag :location_id, options_for_select([['Houston','0'],['San Francisco','1'],['Santiago','2'], ['Valparaiso','3'], ['Rio de Janeiro','4'], ['Milan','5'], ['Palo Alto','6'], ['Las Vegas','7'], ['New York','8'], ['San Diego','9']]), class: "form-control" %>
</div>
<br>
<br>
<div class="actions">
<span itemprop="significantLink">
<%= submit_tag 'Search', :name => nil, class: "btn btn-default" %>
</span>
</div>
</form>
<% end %>
我的位置。rb
def index
@places = Place.search(params[:kind],[:location_id])
end
def self.search(kind, location_id)
return scoped unless kind.present? || location_id.present?
where(['kind = ? AND location_id = ?', kind, location_id])
end
<%= form_tag places_path, :method => 'get' do %>
<form class="form-inline text-center" role="form" action="/places">
<div class="form-group">
<%= label_tag :kind %>
<%= select_tag :kind, options_for_select([['beer','0'],['chocolate','1'],['cocktail','2'], ['coffee','3'], ['tea','4'], ['wine','5'], ['juice','6']]), class: "form-control" %>
/div>
<br>
<div class="form-group">
<%= label_tag :location_id %><br>
<%= select_tag :location_id, options_for_select([['Houston','0'],['San Francisco','1'],['Santiago','2'], ['Valparaiso','3'], ['Rio de Janeiro','4'], ['Milan','5'], ['Palo Alto','6'], ['Las Vegas','7'], ['New York','8'], ['San Diego','9']]), class: "form-control" %>
</div>
<br>
<br>
<div class="actions">
<span itemprop="significantLink">
<%= submit_tag 'Search', :name => nil, class: "btn btn-default" %>
</span>
</div>
</form>
<% end %>
和我的搜索表单\u home.html.erb
def index
@places = Place.search(params[:kind],[:location_id])
end
def self.search(kind, location_id)
return scoped unless kind.present? || location_id.present?
where(['kind = ? AND location_id = ?', kind, location_id])
end
<%= form_tag places_path, :method => 'get' do %>
<form class="form-inline text-center" role="form" action="/places">
<div class="form-group">
<%= label_tag :kind %>
<%= select_tag :kind, options_for_select([['beer','0'],['chocolate','1'],['cocktail','2'], ['coffee','3'], ['tea','4'], ['wine','5'], ['juice','6']]), class: "form-control" %>
/div>
<br>
<div class="form-group">
<%= label_tag :location_id %><br>
<%= select_tag :location_id, options_for_select([['Houston','0'],['San Francisco','1'],['Santiago','2'], ['Valparaiso','3'], ['Rio de Janeiro','4'], ['Milan','5'], ['Palo Alto','6'], ['Las Vegas','7'], ['New York','8'], ['San Diego','9']]), class: "form-control" %>
</div>
<br>
<br>
<div class="actions">
<span itemprop="significantLink">
<%= submit_tag 'Search', :name => nil, class: "btn btn-default" %>
</span>
</div>
</form>
<% end %>
“get”do%>
/div>
无,类别:“btn btn默认值”%>
非常感谢您的帮助!环顾了一大群SO/铁路工人,不知道该怎么办 首先,我觉得有些不对劲。将索引定义为:
def index
@places = Place.search(params[:kind], params[:location_id])
end
此外,您可能希望实际使用respond_块来协商响应应该是什么,例如html或json
respond_to do |format|
format.html { YOUR STUFF HERE }
format.json { YOUR STUFF HERE }
end
我最好的建议是在@places赋值之后放一个
debugger
语句,这样您就可以实际调试正在发生的事情,并查看正在发生的事情 在您的代码中,您在哪里使用设置为搜索结果的@places
变量?在my index.html.erb/places中,谢谢!成功了!你是说像这样的事吗?我对编码很陌生,所以有点困惑。def index@places=Place.search(params[:kind],params[:location_id])respond_to do | format | if@Place.search format.html{redirect_to@places}format.json{redirect_to@places}else format.html{redirect_to@places.all}format.json{redirect_to@places.all}我首先在@后面加上空格。读取从视图传递的参数时,需要始终使用params[:VALUE]。值总是作为散列从表单传递回控制器。不管它是GET、POST、PUT、Form还是Querystring。