ruby-on-rails 新的搜索表单部分被重定向到我的主索引页的模型

kknvjkwl  于 2022-11-19  发布在  Ruby
关注(0)|答案(1)|浏览(112)

我有一个事件模型和一个地址字段,在其中实现了一些地理定位逻辑。在我的主索引页面上,我呈现了一个搜索表单partial,它可以很好地搜索我的数据库中的所有事件。
以下是原始搜索表单:

<%= search_form_for @q,  
    data: { controller: "places", action: "google-maps-callback@window->places#initMap" } do |f| %>
    <div class="formlook">
        <div class="event-search-container">
            <div class="event-name">
                <%= f.label "Event name", class: "form-label" %>
                <%= f.search_field :name_cont, placeholder: "Any event", class: "form-control" %>
            </div>
            <div class="geolocation">
                <%= f.label "Geolocation", class: "form-label" %>
                <%= f.search_field :address_or_city_or_state_or_country_or_continent_cont_all, class: "form-control", placeholder: "Search by continent, country, state, or city", data: { places_target: "field" } %>
            </div>
            <div class="dancestyle">
                <%= f.label "Dance style", class: "form-label" %>
                <%= f.select(:dance_styles_id_in, DanceStyle.all.pluck(:name, :id), { :include_blank => "All dance styles" }, { class: "form-select", id: "select-dancestyle", multiple: true, placeholder: "Any dance style" }) %>
            </div>
            <div class="eventtype">
                <%= f.label "Event Type", class: "form-label" %>
                <%= f.select :event_type_id_in, EventType.all.pluck(:name, :id), { :include_blank => "All event types" }, { id: "select-artist", multiple: true, placeholder: "Any event type", class: "form-select" } %>
            </div>
            <div class="month">
                <%= f.label "Month", class: "form-label" %>
                <%= f.select(:event_month_in, (Date::MONTHNAMES[1..12]), { prompt: "Any month" }, { id: "select-month", :include_blank => "Any month", class: "form-select", multiple: true }) %>
            </div>
            <div class="year">
                <%= f.label "Year", class: "form-label" %>
                <%= f.select :event_year_in, Date.today.year-2 .. Date.today.year+3, { prompt: "Any year" }, { id: "select-year", include_blank: true, class: "form-select", multiple: true }  %>
                <br>
            </div>
        </div>
     </div>
    <%= f.submit "Search!", class: "button-orange" %>
<% end %>

我现在正在为特定的城市创建特定的事件索引页面。所以我希望能够使用我的主索引页面中的其他搜索参数,而不需要名称或地址字段。所以我创建了一个新的html erb文件,该文件具有基于特定城市的事件模型的新范围:

<div class="container index">
  <div>
    <h1>Dance Events in Austin, Texas!</h1>  
      <br>
    <div class="help-attn">
        <p>All of the search options work together! Create your own search filters to find the events you want!</p>
    </div>
       <%= render 'events/local_search_form' %>
        <br>
          <h5>Currently displaying <%= pluralize(@events.in_austin.count, "event") %>. </h5>
        <br>
  </div>

   <h3>Upcoming Events</h3>
  <div class="event-list-wrapper">
    <% @events.upcoming_events.in_austin.each do |event| %>
      <%= render 'event', event: event %>
    <% end %>
  </div> 

  <h3>Past Events</h3>
  <div class="event-list-wrapper past-event">
    <% @events.past_events.in_austin.each do |event| %>
      <%= render 'event', event: event %>
    <% end %>
  </div>

我还需要使用与def index相同的参数将def atx_index添加到我的事件模型中,因为在我看来,它们都是索引页,只是其中一个的作用域不同。
从上面你会看到,我创建了一个不同的搜索表单部分,以反映两个字段,我不再需要了,这里是新的搜索表单部分。

<%= search_form_for @q do |f| %>
    <div class="formlook">
        <div class="local-search-container">
            z <%= f.hidden_field :address_or_city_or_state_or_country_or_continent_cont_all, value: [""] %>
            <div class="dancestyle">
                <%= f.label "Dance style", class: "form-label" %>
                <%= f.select(:dance_styles_id_in, DanceStyle.all.pluck(:name, :id), { :include_blank => "All dance styles" }, { class: "form-select", id: "select-dancestyle", multiple: true, placeholder: "Any dance style" }) %>
            </div>
            <div class="eventtype">
                <%= f.label "Event Type", class: "form-label" %>
                <%= f.select :event_type_id_in, EventType.all.pluck(:name, :id), { :include_blank => "All event types" }, { id: "select-artist", multiple: true, placeholder: "Any event type", class: "form-select" } %>
            </div>
            <div class="month">
                <%= f.label "Month", class: "form-label" %>
                <%= f.select(:event_month_in, (Date::MONTHNAMES[1..12]), { prompt: "Any month" }, { id: "select-month", :include_blank => "Any month", class: "form-select", multiple: true }) %>
            </div>
            <div class="year">
                <%= f.label "Year", class: "form-label" %>
                <%= f.select :event_year_in, Date.today.year-2 .. Date.today.year+3, { prompt: "Any year" }, { id: "select-year", include_blank: true, class: "form-select", multiple: true }  %>
                <br>
            </div>
        </div>
     </div>
    <%= f.submit "Search!", class: "button-orange" %>
<% end %>

因此,特定城市的事件索引页显示正确的范围,但当我使用搜索我被重定向到主事件索引页与搜索参数,我输入.我不明白两个不同的搜索表单部分都链接到主事件索引页.
如何获取特定城市索引页面的新搜索表单部分,以在其相应页面上显示搜索结果?

dluptydi

dluptydi1#

我的重定向表单问题的问题是,我需要设置我创建的新表单的URL,这样我就能够从路由中获得正确的路径名。
我最初的实现虽然有我的配置的形式特别,因为我想重用的形式在多个页面上为个别地方城市,我很幸运地找到了一个旧的7年前的SOF文章,其中提到设置url在渲染行如下:
<%= render 'events/local_search_form', { url: atx_index_path } %>
然后在local_search_form中调用该URL字符串,如下所示:

<% if local_assigns.has_key? :url %>
  <%= search_form_for @q, { url: url } do |f| %>

现在我可以简单地改变我为每个城市创建的每个html.erb文件的url路径!我不知道本地分配!

相关问题