2013-08-30 41 views
0

如何檢測當前頁面是否以Rails中的「.foo」結尾?Rails:檢測當前頁面是否以「/ foo」結尾

基本上,我想根據網址中傳遞的內容在我的視圖中顯示不同的內容。 類似<% if !current_page?('something/something/foo') %>但更具動態性

回答

1

在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 %> 

在Rails 3:

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

我假設你正在討論根據請求的文件類型的不同情況進行響應。通常情況下,這是respond_to是:

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

否則,如果你真的想只是做東西的視圖中,做什麼zeantsoi顯示。這只是一種不規則。如果我更瞭解用例,我會更好地回答你的問題。

1

你可以找到here如何獲得完整的URL 道岔尖軌版本使用的是,則此網址後你只需要分割它與/然後得到.last像這樣的最後一個元素:

url.split("/").last 

我覺得這是簡單的方法

相關問題