簡體   English   中英

Form Rails / Gem Geocoder的渲染結果

[英]Render result for a Form rails / gem geocoder

當用戶單擊“搜索”按鈕時,我想呈現表單的結果。


我想我缺少模型(我真的不知道我是否必須在用戶模型上添加一些東西)或HTML。

我有一個控制器:Searchscontroller

class SearchsController < ApplicationController
  def index
    if params[:city].present?
      @searchs = User.near(params[:city], params[:distance] || 10, order: :distance)
    else
      @searchs = User.all
    end
  end

和search.html.erb中的表格

<%= form_tag search_path, method: :get do %>
  <%= label :user, "Search User near : " %>
  <%= text_field_tag :city, params[:city] %>

  <%= label :distance, "Distance : " %>
  <%= text_field_tag :distance, params[:distance] %>

  <%= submit_tag "Search" %>
<% end %>

  #NOT SURE WITH THAT PART, I think its not working, just the %else is displayed#
    <div>
    <% if @searchs.present? %>
      <%= render @searchs %> # it might be @users instead ? #
    <% else %>
      <p>Oops no user around <%= params[:city] %>.</p>
    <% end %>
    </div>

我的用戶已經找到了寶石地理編碼器,那部分很好,我得到了緯度和經度(以及DB中的城市,它在控制台User.Near('City')中運行),但是我只想顯示一個結果列表,當我單擊表單時像這樣:

倫敦周圍/距離10公里=>>搜索<
結果
> @用戶1
> @用戶2

我希望有人能得到我的答案,在此先多謝

Route.rb

Rails.application.routes.draw do
 devise_for :users
 resources :users, only: [:index, :show]
 root "pages#home"
 get "search" => "pages#search"

因此,基本上,我更改了搜索文件的文件夾>“ searchs / search.html.erb”,然后更改了Route.rb中的路由: get "search" => "searchs#search"

     <%= form_tag search_path, method: :get do %>
      <%= label :user, "Search User near : " %>
      <%= text_field_tag :city, params[:city] %>

      <%= label :distance, "Distance : " %>
      <%= text_field_tag :distance, params[:distance] %>

      <%= submit_tag "Search" %>
    <% end %>

該類仍然是相同的,只是將def“ index ”重命名為“ search ”:

    class SearchsController < ApplicationController
        def search
        if params[:city].present?
          @searchs = User.near(params[:city], params[:distance] || 10)
        else
          @searchs = User.all
        end
      end
    end

並且主要的問題是表單末尾的div以顯示結果(先前的代碼正在尋找部分_search.html.erb ,但我沒有創建它,因此我編寫了,以顯示的結果直接形成。

    <div>
     <table>
      <tr>
      <th>Nickname</th>
      <th>City</th>
      </tr>
        <% @searchs.each do |search| %>
            <tr>
            <td><%= search.nickname %></td>
            <td><%= search.city %></td>
            </tr>
    <% end %>
      </table>
    </div>

我仍然不知道如何僅當我單擊“ 搜索 ”按鈕時才顯示結果(它現在正在刷新表單),但是我對結果使用Partial _form有一個想法。 如果有人對此有解決方案,請在評論中添加它;)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM