【问题标题】:Render result for a Form rails / gem geocoderForm rails / gem geocoder 的渲染结果
【发布时间】:2016-08-17 13:14:50
【问题描述】:

我想在用户单击“搜索”按钮时呈现我的表单结果。


我认为我缺少模型(我真的不知道是否必须在我的用户模型上添加某些内容)或 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>

我的用户已经使用 gem geocoder 定位,这部分很好,我得到了纬度和经度(以及 DB 中的城市,它在控制台 User.Near('City') 中工作),但我只是想显示结果列表,就像我点击表单时这样:

伦敦周边/距离 10 公里 => >搜索结果
> @user 1
> @user 2

希望有人能帮我解答,非常感谢

Route.rb

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

【问题讨论】:

  • 您能否发布您的routes.rb 文件并将您的def index 操作重命名为def search,看看是否有帮助?
  • 我会用我的 route.rb 更新问题,我现在会尝试重命名,谢谢你的回答!
  • 看起来您可能还需要为您的 searchs#search 操作添加路由。尝试添加类似get '/city_search', to: 'searchs#search 的内容。如果你想用这个聊天,我可以更详细地解释这一切是如何工作的,我很乐意这样做。
  • 谢谢 Anthony,我会尝试为 searchs#search 添加一个路由,我认为我在 html 中的代码是错误的 和渲染。如果我找不到任何解决方案,我会直接问你,再次感谢!!
  • 我找到了解决方案,我像你说的那样改变了 route.rb。我更改了我的 html 表单的路径('searchs / search.html.erb')。我改变了渲染,它是部分的,所以我用 ~" "替换它~现在的问题是表格总是显示,但它工作了,当我改变参数,表格随着正确的值而改变。只有当我按下搜索按钮时,我才需要检查如何呈现表格。再次感谢安东尼

标签: ruby-on-rails ruby forms ruby-on-rails-4


【解决方案1】:

所以基本上,我更改了搜索文件的文件夹 > “搜索/search.html.erb” 以及 Route.rb 中的路线:get "search" =&gt; "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 有了一个想法结果。如果有人对此有解决方案,请将其添加到评论中;)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多