Skip to content

fixes #15250 - ignore current controller from link_to/hash_for#3564

Closed
domcleal wants to merge 1 commit intotheforeman:developfrom
domcleal:15250-hash-for-plugins
Closed

fixes #15250 - ignore current controller from link_to/hash_for#3564
domcleal wants to merge 1 commit intotheforeman:developfrom
domcleal:15250-hash-for-plugins

Conversation

@domcleal
Copy link
Copy Markdown
Contributor

@domcleal domcleal commented Jun 2, 2016

When link_to generates a link on a page served from a nested controller
(e.g. foreman_example/examples) with a hash of controller/action from
our hash_for_*_path extension, it calls url_for which added the current
controller prefix to the controller name, and this wouldn't exist.

When linking to hash_for_hosts_path (:controller => 'hosts', :action => 'index'), this would attempt to link to :controller => 'foreman_example/hosts' instead. By returning :use_route from hash_for
as in Rails 4.1, actionpack assumes the controller name is absolute and
will not add the current controller prefix.

Rails source where relative controller is assumed by url_for if no named
route was given (the :use_route option):
https://github.com/rails/rails/blob/v4.2.6/actionpack/lib/action_dispatch/routing/route_set.rb#L695-L698

Rails 4.1 source where :use_route was always added:
https://github.com/rails/rails/blob/v4.1.14.2/actionpack/lib/action_dispatch/routing/route_set.rb#L289-L294

@domcleal domcleal added the 1.12.0 label Jun 2, 2016
@theforeman-bot
Copy link
Copy Markdown
Member

There were the following issues with the commit message:

  • commit message for 3098a9fa7f827df6ad5b81459d3bb3fce694caf8 is not wrapped at 72nd column
  • commit message for 3098a9fa7f827df6ad5b81459d3bb3fce694caf8 is not wrapped at 72nd column

If you don't have a ticket number, please create an issue in Redmine, selecting the appropriate project.

More guidelines are available in Coding Standards or on the Foreman wiki.


This message was auto-generated by Foreman's prprocessor

Comment thread test/unit/hash_for_test.rb Outdated
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a test for the pre-existing opts.dup line, explained at https://github.com/theforeman/foreman/pull/3294/files#r55810116.

@mmoll
Copy link
Copy Markdown
Contributor

mmoll commented Jun 2, 2016

Fixes the reported issue with foreman_salt for me 👍

@domcleal
Copy link
Copy Markdown
Contributor Author

domcleal commented Jun 2, 2016

There were the following issues with the commit message:

I think the commit message is quite reasonable, so have submitted a PR to prprocessor to change the behaviour around URLs: theforeman/prprocessor#43

When link_to generates a link on a page served from a nested controller
(e.g. foreman_example/examples) with a hash of controller/action from
our hash_for_*_path extension, it calls url_for which added the current
controller prefix to the controller name, and this wouldn't exist.

When linking to hash_for_hosts_path (`:controller => 'hosts', :action =>
'index'`), this would attempt to link to `:controller =>
'foreman_example/hosts'` instead. By returning :use_route from hash_for
as in Rails 4.1, actionpack assumes the controller name is absolute and
will not add the current controller prefix.

Rails source where relative controller is assumed by url_for if no named
route was given (the :use_route option):
https://github.com/rails/rails/blob/v4.2.6/actionpack/lib/action_dispatch/routing/route_set.rb#L695-L698

Rails 4.1 source where :use_route was always added:
https://github.com/rails/rails/blob/v4.1.14.2/actionpack/lib/action_dispatch/routing/route_set.rb#L289-L294
@domcleal domcleal force-pushed the 15250-hash-for-plugins branch from 3098a9f to 4dd2eb6 Compare June 2, 2016 13:34
@theforeman-bot
Copy link
Copy Markdown
Member

There were the following issues with the commit message:

  • commit message for 4dd2eb6 is not wrapped at 72nd column
  • commit message for 4dd2eb6 is not wrapped at 72nd column

If you don't have a ticket number, please create an issue in Redmine, selecting the appropriate project.

More guidelines are available in Coding Standards or on the Foreman wiki.


This message was auto-generated by Foreman's prprocessor

Comment thread app/services/menu/item.rb

def url
add_relative_path(@url || @context.routes.url_for(url_hash.merge(:only_path=>true)))
add_relative_path(@url || @context.routes.url_for(url_hash.merge(:only_path=>true).except(:use_route)))
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes the menu system (and its integration tests), as the url_for call here won't use :use_route so it was getting appended as a query parameter.

@dLobatog
Copy link
Copy Markdown
Member

dLobatog commented Jun 2, 2016

Merged as 0cdd659, thanks @domcleal!

@dLobatog dLobatog closed this Jun 2, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants