including url parameters in if statement

2019-08-10 08:10发布

I'm trying to have different parts of my layout included in different pages, I currently have this:

- if %w(/ /news).include? request.fullpath
  = yield      

- unless %w(/ /news).include? request.fullpath

which means when the URL is the homepage or the news page, the content is shown. I need this to include when there is a parameter in the homepage URL eg /?campaign=test I've tried a few things but can't get it to recognise separate parameters.

Answer:

- if params[:controller] == 'application' && params[:action] == 'index' && params[:campaign].present?
  = yield      

- elsif params[:controller] == 'application' && params[:action] == 'index'
  = yield

- else

1条回答
甜甜的少女心
2楼-- · 2019-08-10 08:58

Instead of request fullpath you can take params like

  if params[:controller] == 'news'
  if params[:action] == 'smth'
  if params[:campaign] == 'test'

in your conditions.

UPD

- if params[:controller] == 'homepage'
  = 'This is homepage'
- if params[:controller] == 'homepage' && params[:campaign].present?
  = 'this homepage is with campaign.'

This definetely should work.

查看更多
登录 后发表回答