I currently have a page on my rail site that will show, by default, 25 results per page using will_paginate. I would, however, like to have a dropdown menu that allows the user to select the number of results shown from a predetermined list of values. I looked at the answer to a similar question here but the problem I have with the top solution is that I also have a search field in the same page and changing the number of results per page thus removes the search parameter if there is one.
I was wondering if there was any way for me to get around this problem? Thanks in advance!
The select box proved to be troublesome but i found a different way to do the same functionality.
So by using anchor tags and grabbing the search params when building the path it preserves the search criteria as well as any new per_page value.
so 5 and 10 page links would look like this
<%= Link_to "5", current_path(:per_page =>5,:search => params[:search])%>
<%= Link_to "10", current_path(:per_page =>10,:search => params[:search])%>
and the URL in the href will look like this: whatever.com/current?per_page=10&search=DevOps changing with whatever search term is provided.
Be sure to add the per_page value to the controller as well
per_page = params[:per_page]
#items = Item.all.paginate(page: params[:page], per_page: per_page)
Try this:
In your view -
<%= form_tag xyz_path, method: :get do %>
<%= text_field_tag :search, params[:search] %>
<%= select_tag :per_page, options_for_select([10, 20, 50, 100], params[:per_page]) %>
<%= button_tag :submit %>
<% end %>
In your controller -
unless params[:per_page].present?
params[:per_page] = 20 #default
end
if params[:search].present?
ModelName.where(query: params[:search]).paginate(:page => params[:page], :per_page => params[:per_page])
else
ModelName.all.paginate(:page => params[:page], :per_page => params[:per_page])
end
Related
I'm working on the edit view for a model, and I have several option tags.
I want to see the current value displayed in the option tag and to be able to change it in case I want to.
<%= f.select :base_agradecimiento, options_for_select([#student.base_agradecimiento, $paquete[:base][:tipo].each do |x| [x] end]),{}, {:id => 'req_agrad'} %>
I'm trying to show the current value of base_agradecimiento and the other options in case I want to edit it but it doesn't seem to work. All I can see is the current value and not the array of options.
How can I make this work?
I think I see what you're after. Try this...
<%= f.select :base_agradecimiento, [#student.base_agradecimiento, #paquete[:base][:tipo]].flatten, {}, {:id => 'req_agrad'} %>
Usually, adding an ID tag at the end of a rails form helper, does the trick. However, this does not seem to be working for select boxes. What am I doing wrong?
<%= form_for(#song) do |f| %>
<%= f.select :category, [['Pop', 1], ['Rap', 2]] , :id=>"choose-category"%>
<% end %>
^The ID is not getting set here properly, what am I doing wrong?
Thanks.
The select method takes four arguments, html_options is the fourth, therefore you have to pass an empty hash as the third parameter (options):
f.select(:category, [['Pop', 1], ['Rap', 2]], {}, :id => "choose-category")
i want to categorise my books and create a dropdown and then i want to show only the books for the selected category.
i have created a dropdown using the following code:
<% form_for :category, :url => { :action => :cat_disp } do |f| %>
<%= f.select(:category, Categories.all.map {|p| [p.name,p.id]}, :prompt => "Select a category") %>
<%end%>
how can i show the books only for category selected from dropdown?
Any help will be appreciated.
I'm not sure I really understand what you want. It would probably be easiest to use jQuery's change() method to submit the form to a page that only displays books of one category.
$(function(){
$(dropdown_id).change(function(){
$(this).parent('form').get(0).submit();
});
});
I have a form below displayed on /parties and would like to take whatever a user inputs in this form and add it to the url. I.e. if they searched for "hello" they would be redirected to parties/hello
<h1>Search for a Party</h1><br>
<%= form_tag({:action => "search"}, {:method => "get"}) do %>
<%= label_tag :q, "Enter Playlist Code:" %>
<%= text_field_tag(:q) %>
<%= submit_tag("Find Playlist", :name => "submit") %>
<% end %>
What is the best way to do this?
The logic would go into the PartyController.
Something like this in (syntax may be a little off):
PartyController.rb
def search
result = params[:q]
redirect_to '/parties/' + result
end
yeenow123 gave you a right way, I just want to clarify some points.
"parties/hello" will confuse the routing with parties/:id (show action)
If you don't want to use show action, you should except this from your route.rb: parties resource or you can use show action as function which has params[:id] is your search key and keep working on this.
I'm using the generic search form, and my url after the search looks like
http://localhost:3000/search?commit=Search&page=2&query=feature&utf8=%E2%9C%93
The search works fine, but I would like to remove the default "utf8=✓" and "commit=Search" parameters from the URL, I'm also using will_paginate and I would like the &page=2 to be after the query parameter leaving it like this:
http://localhost:3000/search?query=feature&page=2
My code:
#posts_controller.rb
def search
query = '%'+params[:query]+'%'
#posts = Post.find(:all, :conditions => ["content LIKE ? or title LIKE ?", query, query]).paginate(:page => params[:page], :per_page => 5)
end
and
#html form
<%= form_tag(search_path, :method => 'get') do %>
<%= text_field_tag "query" %>
<%= submit_tag "Search" %>
<% end %>
and
#routes.rb
match '/search', :to => 'posts#search'
Thanks.
See similar questions:
Rails 3 UTF-8 query string showing up in URL?
removing "utf8=✓" from rails 3 form submissions
Basically, to remove 'commit=Search' add :name => nil to the submit_tag. IE needs the utf8 character. However, the second link has a initializer method to remove that part.
In this video, Ryan Bates talks about the name: nil fix (without ajax): http://railscasts.com/episodes/37-simple-search-form
You cant just remove it from url as far as YOU send it.
To clean up will_paginate try this
<%= will_paginate #whatever, params => params.merge({:commit => nil, :utf8 => nil}) %>
I solved utf problem by using
<form action="<%= root_path %>" method="get" >
...
</form>
instead of form_tag, it solved it.
Ryan Bates did a nice screen cast on exactly what you're trying to do (plus some more).
http://railscasts.com/episodes/240-search-sort-paginate-with-ajax