ruby on rails - Merge link and select params -
in case have link params , select params
if filter @posts
select (:per_page)
, (:pub)
params ok, because used params.merge
in link.
but if want use first link , select, not work, because don't know should write params.merge
in select.
code controller:
def index @posts = post @posts = @posts.published unless params[:pub] @posts = @posts.where(:published => params[:pub]) if params[:pub] @posts = @posts.page(params[:page]).per(params[:per_page] || 5) end
code view
<%= select_tag :per_page, options_for_select(%w(1 2 3), params[:per_page].to_i), :onchange => "if(this.value){window.location='?per_page='+this.value;}" %> <%= link_to "unpubl", params.merge(:pub => :f) %> <%= link_to "publ", params.merge(:pub => :t) %>
if understood correctly trying create kind of html filters pass controller right ?
if case, should build html filters using form
. way not have deal "merging" options params
hash. form take care of you.
example:
<%= form_tag posts_path, method: :get %> <%= select_tag :per_page, options_for_select(%w(1 2 3), params[:per_page].to_i) %> <%= check_box_tag 'published', 'true' %> <%= submit_tag 'submit' %> <% end %>
then in controller:
params[:published] params[:per_page]
will hold corresponding values.
more information form_tag
here
Comments
Post a Comment