0

I have a helper_method that allows links to escape from a subdomain. However it is impacting my videos_controller, as it essentially seems to negate the 'current_event' method when not in the events controlller.

I've tried several dozen different ways over the last 4 days to make it so I can still escape my links from the subdomain, but still allow the videos_controller to work.

I think the best way to achieve this is to exclude the videos_controller from the helper method, but I'm not sure how (or if it is actually the best way forward - I'm obviously a noob!) Any suggestions please?! Relevant code below:

module UrlHelper
  def url_for(options = nil)
      if request.subdomain.present? and request.subdomain.downcase != 'www' and !options.nil? and options.is_a?(Hash) and options.has_key? :only_path and options[:only_path] 
        options[:only_path] = false 
      end
      super
  end
end

Videos_controller

def new
    if current_event?
      @video = current_event.videos.new
    else
      @video = Video.new
    end
  end

  def create
    if current_event.present?
      @video = current_event.videos.new(params[:video])
      @video.user_id = current_user.id
       key = get_key_from_the_cloud
      @video.key = key
    else
      @video = current_user.videos.new(params[:video])
      @video.user_id = current_user.id
      key = get_key_from_the_cloud
      @video.key = key
    end
    if @video.save
      flash[:success] = "Video uploaded!"
      redirect_to root_url(subdomain: => current_event.name)
    else
      flash[:error] = "#{@video.errors.messages}"
      render :new
    end
  end

current_event method

  def current_event
    if request.subdomain.present?
      @event = Event.find_by_name(request.subdomain)
    end
  end
jfdimark
  • 2,239
  • 1
  • 14
  • 27
  • Did you take a look at [this post](http://stackoverflow.com/questions/1179865/why-are-all-rails-helpers-available-to-all-views-all-the-time-is-there-a-way-t) yet? – Vikko Dec 19 '12 at 12:32
  • I hadn't but I've now tried this and it hasn't helped solve the problem. For some reason I can't just include the urlhelper in my events controller (and I'm pretty sure I'm trying to do it correctly 'include UrlHelper' under the 'EventsController' – jfdimark Dec 19 '12 at 13:59
  • Have you tried if the inclusion works at all? You might want to create a new function `def test` that only does something like `puts "Test is called"` If that works you know its not including that fails but it has to be the method. Otherwise you know the module is not included and you can narrow down the search. – Vikko Dec 19 '12 at 14:27
  • I've established it isn't being included. Only my 'applications helper' is being included, as the test method works when I place it in there but not when I put it in any other helper method and try including it in the events controller. – jfdimark Dec 19 '12 at 14:58
  • Thank you! I've rechecked how I was including the helper method and it was wrong after all, so it is now working locally only on that controller. Quick q on SO best practice - should I now remove this question or leave it? Thx again. – jfdimark Dec 19 '12 at 15:06
  • I added the steps to an answer so others with the same issue don't have to go through all this comment mess ;) – Vikko Dec 19 '12 at 15:17

1 Answers1

0

Did you take a look at this post yet?

You might want to create a new function test that only does something like

module UrlHelper
  def test
    puts "Test is called"
  end
end 

If that works you know its not including that fails but it has to be the method. Otherwise you know the module is not included and you can narrow down the search.

Community
  • 1
  • 1
Vikko
  • 1,396
  • 10
  • 23