有没有办法检查 URL 的一部分是否包含某个字符串 [英] Is there a way to check if part of the URL contains a certain string

查看:42
本文介绍了有没有办法检查 URL 的一部分是否包含某个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法检查 URL 的一部分是否包含某个字符串:

Is there a way to check if part of the URL contains a certain string:

例如.<% if current_spree_page?("/products/*") %>,其中*可以是什么?

Eg. <% if current_spree_page?("/products/*") %>, where * could be anything?

推荐答案

我测试过,gmacdougall 的回答有效,但我已经找到了解决方案.

I tested, and gmacdougall's answer works, I had already found a solution though.

这是我用来根据 url 呈现不同布局的内容:

This is what I used to render different layouts depending on what the url is:

  url = request.path_info
  if url.include?('products')
    render :layout => 'product_layout'
  else
    render :layout => 'layout'
  end

需要注意的重要一点是,不同的页面会在控制器内调用不同的方法(例如 show、index).我所做的是将这段代码放在它自己的方法中,然后在需要的地方调用该方法.

The important thing to note is that different pages will call different methods within the controller (eg. show, index). What I did was put this code in its own method and then I am calling that method where needed.

这篇关于有没有办法检查 URL 的一部分是否包含某个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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