ruby on rails - How to create a form using simple_form to control scopes (via has_scope) in a view -
basically, cannot figure out how prevent form generator encapsulating in hash (filter{...}), making easy form set params used in view's scoping.
has_scope code in controller:
has_scope :degree_id has_scope :discipline_id has_scope :competency_id has_scope :type_id has_scope :year
simple_form code in view:
<%= simple_form_for :filter, url: analyze_school_path, method: :get |f| %> <%= f.error_notification %> <div class="form-inputs"> <%= f.input :y, label: 'year', collection: @years, include_blank: '- year -' %> <%= f.input :discpline_id, label: 'discipline', collection: discipline.all, include_blank: '- discipline -' %> <%= f.input :competency_id, label: 'competency', collection: competency.all, include_blank: '- competency -' %> <%= f.input :type_id, label: 'type', collection: jobtype.all, include_blank: '- type -' %> </div> <div class="form-actions"> <%= f.button :submit, 'filter', class: "btn btn-primary" %> </div> <% end %>
sample output url:
.../analyze/school?utf8=✓&filter%5by%5d=2016&filter%5bdiscipline_id%5d=2&filter%5bcompetency_id%5d=2&filter%5btype_id%5d=1&commit=filter
desired output url:
.../analyze/school?y=2016&discipline_id=2&competency_id=2&type_id=1
1st solution: iterate on hash , set params used scope.
(+) works , simple (-) url still messy (-) seems hacky
params[:filter].each |k,v| params[k] = v end
solution 2: create form using pure html.
(+) url info cleaner (-) code messier , brittler (-) seems hacky
i've googled lot , surprised i've not come across makes easy create forms in conjunction has_scope.
please save me having use 1 of above solutions! thanks!
i think use simple form_tag rails:
see more here: http://guides.rubyonrails.org/form_helpers.html
or implement method like
def parse_filter_params params.merge!(params[:filter]) if params[:filter] end
and apply before each action want:
before_action :parse_filter_params
Comments
Post a Comment