Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RoR Ransack Searching issue

I have two different controllers (scholarships_controller, scholarships_browse_controller) that look at the scholarship model. I want my Ransack search function to show the search results on the current page. So, for example, if I am on /scholarships_browse and I use the search functionality, I want it to use the scholarships_browse_controller and show the results on /scholarships_browse.

Currently, if I search on /scholarships_browse it uses the scholarships_controller and redirects to /scholarships to show the results (instead of /scholarships_browse).

Code for ScholarshipsBrowseController and ScholarshipsController

def index  
    @search = Scholarship.search(params[:q])
    @scholarships = @search.result.page(params[:page]).per(3)  #allows 3 scholarships on a page at a time
    @search.build_condition

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @scholarships }
    end 

end

Code for scholarships browse index.html.erb:

<%= search_form_for @search do |f| %>
    <%= f.condition_fields do |c| %>
        <div class="field">
            <%= c.attribute_fields do |a| %>
                <%= a.attribute_select %>
            <% end %>
            <%= c.predicate_select compounds: false, only: [:cont, :eq, :gt, :lt] %>
            <%= c.value_fields do |v| %>
                <%= v.text_field :value %>
            <% end %>
        </div>
    <% end %>
    <div class="actions"><%= f.submit "Search" %></div>
<% end %>

So, I guess specifically I'm asking how do I make sure I am using the ScholarshipsBrowseController index instead of ScholarshipsController index when I am on /scholarships_browse ?

like image 849
Nicoleyyz Avatar asked Feb 28 '13 22:02

Nicoleyyz


1 Answers

On your view:

<%= search_form_for @search, url: RAILS_ROUTEHERE do |f| %>
...
<%- end %>

search_form_for its an extension for form_for, so you can use the :url parameter to tell the form what should be the action of it (you can check the page source code when you render it on browser, you can check the tag <form action=""> to make sure it points to the right route.

like image 99
rorra Avatar answered Oct 22 '22 08:10

rorra