简体   繁体   English

如何使用simple_form创建表单以控制视图中的范围(通过has_scope)

[英]How to create a form using simple_form to control scopes (via has_scope) in a view

Basically, I cannot figure out how to prevent the form generator from encapsulating everything in a hash (filter{...}), thus making it easy for the form to set the params used in the view's scoping. 基本上,我无法弄清楚如何防止表单生成器将所有内容封装在哈希中(过滤器{...}),从而使表单很容易设置视图范围内使用的参数。

has_scope code in controller: 控制器中的has_scope代码:

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代码:

<%= simple_form_for :filter, url: analyze_school_path, method: :get do |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: 样本输出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 ... /分析/校?UTF8 =✓&滤波器%5BY%5D = 2016&滤波器%5Bdiscipline_id%5D = 2&滤波器%5Bcompetency_id%5D = 2&滤波器%5Btype_id%5D = 1&提交=过滤

Desired output URL: 所需的输出网址:

.../analyze/school?y=2016&discipline_id=2&competency_id=2&type_id=1 ... /分析/学校?Y = 2016&discipline_id = 2&competency_id = 2&TYPE_ID = 1

1st Solution: Just iterate over the hash and set the params used by scope. 第一个解决方案:只需遍历哈希并设置范围使用的参数。

(+) This works and is fairly simple (-) The URL is still messy (-) This seems hacky (+)这很有效并且非常简单(-)URL仍然很凌乱(-)似乎很黑

params[:filter].each do |k,v|
   params[k] = v
end

Solution 2: Create the form using pure HTML. 解决方案2:使用纯HTML创建表单。

(+) URL info is cleaner (-) Code is messier and brittler (-) This seems hacky (+)URL信息更干净(-)代码更混乱,更脆弱(-)这似乎很hacky

I've Googled a lot and am surprised that I've not come across something that makes it easy to create forms in conjunction with has_scope. 我已经在Google上搜索了很多东西,但感到惊讶的是,我没有遇到可以轻松地与has_scope一起创建表单的东西。

Please save me from having to use one of the above solutions! 请不必使用以上解决方案之一,救救我! Thanks! 谢谢!

I think you could use the simple form_tag by rails: 我认为您可以通过rails使用简单的form_tag:

See more about that here: http://guides.rubyonrails.org/form_helpers.html 在此处查看有关此内容的更多信息: http : //guides.rubyonrails.org/form_helpers.html

Or just implement a method like 或者只是实现像

def parse_filter_params
  params.merge!(params[:filter]) if params[:filter]
end

and apply it before each action you want: 并在您要执行的每个操作之前应用它:

before_action :parse_filter_params

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM