Rails:检测当前页面是否以“/ foo”结尾

时间:2022-04-25 06:30:34

How would you detect if the current page ends in ".foo" in Rails?

你如何检测当前页面是否以Rails中的“.foo”结尾?

Basically, I'd like to show different things in my view based on what's passed in the url. Something like <% if !current_page?('something/something/foo') %> but more dynamic

基本上,我想根据网址中传递的内容在我的视图中显示不同的内容。类似于<%if!current_page?('something / something / foo')%>但更具动态性

3 个解决方案

#1


1  

In Rails 3.2+:

在Rails 3.2+中:

# in your controller
url = request.original_url
@ext = File.extname(url) #=> .foo

# in your view
<% if @ext == '.foo' %>
    # do this
<% else %>
    # do that
<% end %>

In Rails 3:

在Rails 3中:

# to retrieve the fully qualified URL
url = "#{request.protocol}#{request.host_with_port}#{request.fullpath}"

#2


1  

I'm assuming you're talking about responding with different things depending on the file type requested. Typically, this is what respond_to is for:

我假设你正在谈论根据所请求的文件类型响应不同的东西。通常,这就是respond_to用于:

class SomeController < ApplicationController
  def some_action
    respond_to do |format|
      format.html { render :new }
      format.foo { render :foo }
    end
  end
end

Otherwise if you really want to just do stuff inside the view, do what zeantsoi showed. This is just kind of irregular. If I knew more about the use case I'd be better able to answer your question.

否则,如果你真的想在视图中做一些事情,那就做zeantsoi所展示的。这只是一种不规则的。如果我对用例有更多了解,我会更好地回答你的问题。

#3


1  

you can find here how to get full url switch rails version you are using, then after get this url you just need to split it with / then get the last element with .last like this :

你可以在这里找到如何获得你正在使用的完整url switch rails版本,然后在得到这个url之后你只需要将它拆分为/然后用.last获取最后一个元素,如下所示:

url.split("/").last

i think this is the easy way

我认为这是一种简单的方法

#1


1  

In Rails 3.2+:

在Rails 3.2+中:

# in your controller
url = request.original_url
@ext = File.extname(url) #=> .foo

# in your view
<% if @ext == '.foo' %>
    # do this
<% else %>
    # do that
<% end %>

In Rails 3:

在Rails 3中:

# to retrieve the fully qualified URL
url = "#{request.protocol}#{request.host_with_port}#{request.fullpath}"

#2


1  

I'm assuming you're talking about responding with different things depending on the file type requested. Typically, this is what respond_to is for:

我假设你正在谈论根据所请求的文件类型响应不同的东西。通常,这就是respond_to用于:

class SomeController < ApplicationController
  def some_action
    respond_to do |format|
      format.html { render :new }
      format.foo { render :foo }
    end
  end
end

Otherwise if you really want to just do stuff inside the view, do what zeantsoi showed. This is just kind of irregular. If I knew more about the use case I'd be better able to answer your question.

否则,如果你真的想在视图中做一些事情,那就做zeantsoi所展示的。这只是一种不规则的。如果我对用例有更多了解,我会更好地回答你的问题。

#3


1  

you can find here how to get full url switch rails version you are using, then after get this url you just need to split it with / then get the last element with .last like this :

你可以在这里找到如何获得你正在使用的完整url switch rails版本,然后在得到这个url之后你只需要将它拆分为/然后用.last获取最后一个元素,如下所示:

url.split("/").last

i think this is the easy way

我认为这是一种简单的方法