Jump to content

The ultimate community for Ruby on Rails developers.


Newbie: conditional creation of index view based on user input

  • Please log in to reply
1 reply to this topic

#1 jhbmac



  • Members
  • 2 posts

Posted 18 May 2014 - 04:09 AM

Have need to have the index view to be conditionally recreated/refreshed based on user input.


Current model has multiple fields. Index view is being build with the following entry in the controller:


      @prospects = Prospect.order(last_name: :asc,first_name: :asc).paginate page: params[:page], per_page: 27


What I would like to be able to do is allow the user to enter input into one or more of the current view fields, ie last_name, county, etc. which would then drive the index to be build with something similar to the following:


@prospects = Prospect.where(params[:last_name]).order(last_name: :asc,first_name: :asc).paginate page: params[:page], per_page: 27


where the parameter/value passed to the where clause is based on a non-blank input field thereby reducing the result set accordingly




#2 jhbmac



  • Members
  • 2 posts

Posted 20 May 2014 - 06:21 PM   Best Answer

After some digging I found a way to do this without overly confusing the end user.


Placing parameters on the URL using /controller?parameters=value


and then adding to the index method of the controller the following:


if (params.has_key?(key1:)

 do something

elsif (params.has_key?(key2:)

 do something else


 do the default action



Not sure how to mark this one SOLVED, but for my purposes I consider it solved.



0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users