包括if语句中的url参数 [英] including url parameters in if statement

查看:142
本文介绍了包括if语句中的url参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我的布局的不同部分包含在不同的页面中,我目前有这个:

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

这意味着当URL是主页或新闻页面时,会显示内容。我需要这个包括主页网址中有参数时,例如 /?campaign = test 我尝试了一些但却无法识别单独的参数。

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.

答案:

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

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

- else


推荐答案

您可以使用params
而不是请求完整路径

Instead of request fullpath you can take params like

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

在您的条件下。

UPD

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

此定义应该有效。

这篇关于包括if语句中的url参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆