Scope rails redirect to index

331 views Asked by At

Sorry if this is an easy question, but I've just started to learn Rails.

The app has a scope which works just fine in the Index page.

But I put the search option in the nav bar, so someone can do the search from any page, like show view. I want them to be redirected to the index view with the scope filters.

How do I redirect the user?

Post Controller:

def index
  @posts = Post.where(nil) # creates an anonymous scope
  filtering_params(params).each do |key, value|
    @posts = @posts.public_send(key, value) if value.present?
  end
end

def filtering_params(params)
  params.slice(:size, :animal, :gender, :starts_with)
end

Post Model:

scope :starts_with, -> (name) { where("LOWER(name) like ?", "#{name.downcase}%")}

Application view:

<form class="navbar-form navbar-left" role="search">
  <div class="form-group">
    <%= simple_form_for :post, :method => 'get', :url => "/", :html => {:class => 'form-inline'} do |f| %>
    <%= f.input :starts_with, input_html: { name: 'starts_with', :value => params[:starts_with] }, label: ('Search:'), :required => false, :placeholder => "Animal name" %>
    <%= f.button :submit, ('Search') %>
    <% end %>
  </div>        
</form>
0

There are 0 answers