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
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
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.
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}"