refinerycms icon indicating copy to clipboard operation
refinerycms copied to clipboard

url render error on admin

Open indriq opened this issue 14 years ago • 29 comments

i've found 4 places where the urls don't render well when I don't run the application in the domain's root (2.0.5):

  • admin upper menu (plugin.url)
  • uploaded image preview (both views) (list_view_image.url and image.url)
  • uploaded file download (resource.url)

since those urls don't include a request.script_name they don't work. i saw the url_for in the master branch but since the url_for just returns the parameter without a change if it's a string it doesn't help. I put the request.script_name before those urls and it works for me now.

maybe an override for the url_for would help. if param is str request.script_name would be inserted before the parameter. i'm not sure if it shouldn't be a rails' fix instead...

i've been learning ruby for a week and rails for 4 days so if i speak stupid then sorry. ... and sorry for bad english

indriq avatar Aug 07 '12 05:08 indriq

@ugisozols I've assigned you to investigate this :D

parndt avatar Aug 29 '12 02:08 parndt

Tried to reproduce: image preview and file download isn't working as pointed out by @indriq. Will try to fix.

ugisozols avatar Aug 29 '12 13:08 ugisozols

Note to self:

Refinery mounted at /test.

  • images doesn't show up because image.url returns /system/images/...
  • manually accessing an image and prefixing url with /test shows the image
  • changing Refinery::Images.dragonfly_url_format config from /system/images/:job/:basename.:ext to /test/system/images/:job/:basename.:ext prefixes all urls with /test but images still don't show

ugisozols avatar Sep 07 '12 14:09 ugisozols

Refinery mounted at /test.

When this gets called from dragonfly it passes env['PATH_INFO'] to params_for but env['PATH_INFO'] is /system/images/.... (note that it's missing /test in front of it) so digging deeper I found that /test mount point is passed around with env['SCRIPT_NAME'] so it's not Refinery's issue.

Dragonfly hasn't been updated ~5 months so we probably need to introduce monkeypatching...

ugisozols avatar Sep 10 '12 12:09 ugisozols

# in core/lib/refinery/core/engine.rb
initializer "monkeypatch dragonfly to support mounted engines" do
  Dragonfly::UrlMapper.class_eval do
    alias_method :old_params_for, :params_for

    def params_for(path, query)
      new_path = Refinery::Core::Engine.routes.url_helpers.root_path.sub(/\/$/, "") << path
      old_params_for(new_path, query)
    end
  end
end

?

ugisozols avatar Sep 10 '12 13:09 ugisozols

@markevans hey do you have any thoughts on the above issue and proposed solution?

parndt avatar Sep 10 '12 19:09 parndt

I think the url_mapper is the wrong place to patch it - the url_mapper is a simple way of mapping between params and a path, given a format "/blah/:foo/:blah"

if you look at the way the "url_host" works (in server.rb), if doesn't exist the dragonfly url is "/some/thing" and if it does the url is "url_host/some/thing"

there could be something like "url_path_prefix" that works in the same way e.g. url_path_prefix = "/test", or in this case url_path_prefix = Refinery::Core::Engine.routes.url_helpers.root_path. This url_path_prefix would work very similarly to url_host currently does.

I'm currently (slowly!) making some fairly large changes to Dragonfly and haven't been releasing recently - I'll add this to my TODO list but if you don't mind I won't be releasing yet (I'll do it as part of a bigger release), so you may want to include a patch for now

markevans avatar Sep 11 '12 14:09 markevans

@markevans the main reason for different mount path not working is that here env["PATH_INFO"] gets passed to params_for and because env["PATH_INFO"] doesn't contain mount path it's just easier to patch params_for by basically prepending env["SCRIPT_NAME"] (in Refinery's case I used root_path url helper because it returns the same thing as env["SCRIPT_NAME"]) to env["PATH_INFO"].

Simplified version:

Engine mounted at /test Dragonfly url_format set to /test/images/job_id

Without monkeypatching Dragonfly will see this url as /images/job_id.

@parndt should I send a PR with my proposed patch?

ugisozols avatar Sep 11 '12 17:09 ugisozols

@markevans thanks a lot for taking the time :-) love your work

@ugisozols might as well patch but we need a few more test cases around this than usual if we're going to be monkey patching a third party library like Dragonfly. We'll also need to be able to turn the patch on and off if possible (I'm guessing this may make future releases of Dragonfly act funny).

parndt avatar Sep 11 '12 21:09 parndt

Agreed on specs part but not sure how to make it configurable.

Refinery::Core.config.disable_dragonfly_monkeypatch = true/false

?

Maybe we shouldn't patch it after all and if the user needs this functionality he could add that monkeypatch himself.

ugisozols avatar Sep 12 '12 07:09 ugisozols

Yeah, just document it.

parndt avatar Sep 12 '12 07:09 parndt

2.0.8 appears to be working ok. I'm closing this and if someone have similar issue in the future we can link to my comment.

ugisozols avatar Sep 12 '12 07:09 ugisozols

About this patch, the best way to apply it is to fork the entire project and make the change where is mentioned (in core/lib/refinery/core/engine.rb) ?

Asking this because it's a little uncomfortable approach, if I think of using refinery long term (doing upgrades and all)...Or maybe you know a better way of doing it within a Rails app ?

marklazz avatar May 31 '13 14:05 marklazz

@marklazz you can create something like this in your application:

# config/initializers/dragonfly_monkey_patch.rb
require 'dragonfly/url_mapper'
Dragonfly::UrlMapper.class_eval do
  alias_method :old_params_for, :params_for

  def params_for(path, query)
    new_path = Refinery::Core::Engine.routes.url_helpers.root_path.sub(/\/$/, "") << path
    old_params_for(new_path, query)
  end
end

Of course I have no idea whether it's still needed.

parndt avatar May 31 '13 17:05 parndt

Thanks for the answer @parndt, as far as I can tell is still needed.

But by impl. your suggestion, I've got a "SystemStackError: stack level too deep" error. So, thinking that the only way of doing this is by forking...

marklazz avatar May 31 '13 17:05 marklazz

@marklazz which versions of Refinery + Dragonfly are you using?

parndt avatar May 31 '13 17:05 parndt

I just installed today which lead to this versions on my Gemfile.lock: refinerycms-core (2.0.10) & dragonfly (0.9.15) (just specify this: "gem 'refinerycms', '~> 2.0.0' "on my Gemfile, as described on the instructions)

marklazz avatar May 31 '13 18:05 marklazz

I can't reproduce the stack level too deep problem. Is it possible to temporarily make your application open source or temporarily add me as a collaborator?

parndt avatar May 31 '13 18:05 parndt

My monkeypatch isn't working anymore :sob:

ugisozols avatar May 31 '13 19:05 ugisozols

Oh, that's what I'm guessing right now :(

I'm not able to make this app open source, not to add a collaborator as it doesn't belong to me.

But I was creating a fresh app with refinery and tried that patch, and the stack level too deep issue doesn't show, but the fix doesn't work either.

marklazz avatar May 31 '13 19:05 marklazz