Rails - 当URL中存在参数时显示/隐藏CSS类

时间:2021-09-22 21:32:13

In my view I've a form where I only want to show inputs when its parameters appear in the URL:

在我看来,我有一个表单,我只想在其参数出现在URL中时显示输入:

= form_tag search_path, :method => :get do
  = select_tag 'search[type]', options_for_select([['Publishers', 'publishers'], ['Editions', 'editions'], ['Contributors', 'contributors']], selected = (params[:search][:type] rescue nil)), prompt: "Search type", :required => "true"

  = select_tag 'search[filter]', options_for_select([['Filter by', 'filter_by'], ['All', 'all']], selected = (params[:search][:filter] rescue nil)), prompt: "Select filter", :required => "true"

  - invisible = params[:search][:value].present? ? '' : 'invisible'
  = text_field_tag 'search[value]', (params[:id] || params[:search][:value] rescue nil), :required => "true", :class =>  "#{invisible}", :placeholder => "Type search"

My idea was control it showing a CSS class called "invisible" with display: none;, as above. But when I render the view it returns this Rails error in the line where I declare the "invisible":

我的想法是控制它显示一个名为“invisible”的CSS类,其显示:none;,如上所述。但是当我渲染视图时,它在我声明“不可见”的行中返回此Rails错误:

undefined method `[]' for nil:NilClass

What's wrong?

3 个解决方案

#1


1  

This is because when the form is loading params[:search] is nil, and you're trying to call [:filter] on it, hence undefined method [] for nil.

这是因为当表单加载时,params [:search]为nil,并且你试图在其上调用[:filter],因此未定义的方法[]为nil。

There's various solutions, some nice, some not so nice, pick your favourite:

有各种解决方案,一些不错,一些不太好,选择你最喜欢的:

invisible = params[:search].try(:[], :value) ? '' : 'invisible'
invisible = params[:search] && params[:search][:value] ? '' : 'invisible'
invisible = (params[:search] || {})[:value] ? '' : 'inivisble'
invisible = (params[:search][:value] rescue nil) ? '' : 'invisible'

#2


0  

It is because params[:search] is nil and you are calling params[:search][:filter]

这是因为params [:search]是零并且你正在调用params [:search] [:filter]

Try:

invisible = params[:search].present? ? (params[:search][:filter].present? ? '' : 'invisible') : ''

#3


-1  

...

- invisible = params[:search].try(:[], :filter).blank? ? 'invisible' : ''

...

#1


1  

This is because when the form is loading params[:search] is nil, and you're trying to call [:filter] on it, hence undefined method [] for nil.

这是因为当表单加载时,params [:search]为nil,并且你试图在其上调用[:filter],因此未定义的方法[]为nil。

There's various solutions, some nice, some not so nice, pick your favourite:

有各种解决方案,一些不错,一些不太好,选择你最喜欢的:

invisible = params[:search].try(:[], :value) ? '' : 'invisible'
invisible = params[:search] && params[:search][:value] ? '' : 'invisible'
invisible = (params[:search] || {})[:value] ? '' : 'inivisble'
invisible = (params[:search][:value] rescue nil) ? '' : 'invisible'

#2


0  

It is because params[:search] is nil and you are calling params[:search][:filter]

这是因为params [:search]是零并且你正在调用params [:search] [:filter]

Try:

invisible = params[:search].present? ? (params[:search][:filter].present? ? '' : 'invisible') : ''

#3


-1  

...

- invisible = params[:search].try(:[], :filter).blank? ? 'invisible' : ''

...