0

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

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

goo
  • 2,230
  • 4
  • 32
  • 53

3 Answers3

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:

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.

varatis
  • 14,494
  • 23
  • 71
  • 114
1

In 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:

# to retrieve the fully qualified URL
url = "#{request.protocol}#{request.host_with_port}#{request.fullpath}"
zeantsoi
  • 25,857
  • 7
  • 69
  • 61
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.split("/").last

i think this is the easy way

Community
  • 1
  • 1
medBouzid
  • 7,484
  • 10
  • 56
  • 86