administrate icon indicating copy to clipboard operation
administrate copied to clipboard

Does not play along with model named Host

Open jurgens opened this issue 1 year ago • 3 comments

  • What were you trying to do? it does not play along with a model named Host

  • What did you end up with (logs, or, even better, example apps are great!)? it looks like error is happening when it's trying to build a link in a collection view

link_to[actionview (7.2.2) lib/action_view/helpers/url_helper.rb]
209
        html_options, options, name = options, name, block if block_given?
        options ||= {}
        html_options = convert_options_to_data_attributes(options, html_options)
        url = url_target(name, options)
        html_options["href"] ||= url
        content_tag("a", name || url, html_options, &block)
      end
>> options
=> #<ActionController::Parameters {"host"=>#<ActionController::Parameters {"order"=>:id, "direction"=>:asc} permitted: false>} permitted: true>
>> 
>> url_target(name, options)
!! #<ActionController::UnfilteredParameters: unable to convert unpermitted parameters to hash>
>>
  • What versions are you running?
    • Rails 7.2.2
    • administrate 0.20.1

jurgens avatar Dec 03 '24 11:12 jurgens

I wonder if the URL builder considers host to be a reserved value or something.

If you create a normal Rails controller, do you see the same behaviour?

nickcharlton avatar Dec 03 '24 12:12 nickcharlton

It is not a reserved word, we have HostsController working with Host model in the main application. I suspect administrate overwrites host variable in a controller or interferes in some similar way

jurgens avatar Dec 03 '24 13:12 jurgens

Yeah, quite likely. Would you be able to put together an example app I can clone and try out?

I'm not sure why it would be the case that this fails, but it shouldn't.

nickcharlton avatar Dec 05 '24 12:12 nickcharlton

@nickcharlton it took me a while to put this together but here's a sample project https://github.com/jurgens/administrate-host-example

I created two identical models - Host and Chef in a readme you can find an error message that pops up with you try to navigate to admin/hosts

jurgens avatar Jun 24 '25 20:06 jurgens

would really appreciate a workaround, even if it is a monkey-patch :)

jurgens avatar Jun 24 '25 20:06 jurgens

@jurgens @nickcharlton Hey, I have a solution for this bug.

I believe that it can happen with other names too, like domain or port which is quite annoying.

The bug happens in the app/views/administrate/application/_collection.html.erb view file. There is a line that calls link_to helper with parameters that look like {"host" => {"order" => :id, "direction" => :asc}}. Some method inside Rails thinks that host corresponds to a URL host and not to a URL parameter called host.

There is an easy way to solve it: wrap these parameters into a hash params: ... before calling the link_to method. This way Rails will understand that it's not a url host value.

Here is a PR that fixes the bug in administrate: https://github.com/thoughtbot/administrate/pull/2865

@jurgens if you want to have a local workaround, you can generate the collection view file in your repo and then correct the bug a similar way that I did in my PR.

endenis avatar Jul 05 '25 22:07 endenis

@endenis many thanks, will try it out today!

jurgens avatar Jul 07 '25 09:07 jurgens

Should be fixed now on main after merging https://github.com/thoughtbot/administrate/pull/2865. Thank you all!

pablobm avatar Jul 13 '25 06:07 pablobm