Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#resource and #resources fail quietly and succeed quietly when invalid arguments are passed to :only and :except options respectively #51463

Open
joshuay03 opened this issue Apr 2, 2024 · 1 comment · May be fixed by #51464

Comments

@joshuay03
Copy link
Contributor

joshuay03 commented Apr 2, 2024

This came up when pairing with a dev who is newish to Rails. They assumed that passing a custom action argument to :only when using #resources would auto-magically define a route for it with some defaults. Reading the examples in the documentation for these methods, it's not clear (especially to beginners) what all the accepted arguments for these options are, there are only examples using default actions.

I think it would be beneficial if an error was raised if invalid arguments are passed in.

Steps to reproduce

# frozen_string_literal: true

require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"

  git_source(:github) { |repo| "https://github.com/#{repo}.git" }

  gem "rails", github: "rails/rails", branch: "main"
end

require "action_controller/railtie"

class TestApp < Rails::Application
  config.root = __dir__
  config.hosts << "example.org"
  config.secret_key_base = "secret_key_base"

  config.logger = Logger.new($stdout)
  Rails.logger  = config.logger
end

require "minitest/autorun"
require "rack/test"

class BugTest < Minitest::Test
  include Rack::Test::Methods

  def test_invalid_resources_only_option
    assert_raises(ArgumentError) do # ❌ fails quietly, no routes generated
      app.routes.draw do
        resources :posts, only: :foo
      end

      # => routes: []
      Rails.logger.info("routes: #{app.routes.routes.map { |r| r.defaults[:action] }}")
    end
  end

  def test_invalid_resources_except_option
    assert_raises(ArgumentError) do # ❌ succeeds quietly, all default routes generated
      app.routes.draw do
        resources :posts, except: :foo
      end

      # => routes: ["index", "create", "new", "edit", "show", "update", "update", "destroy"]
      Rails.logger.info("routes: #{app.routes.routes.map { |r| r.defaults[:action] }}")
    end
  end

  private
    def app
      Rails.application
    end
end

Expected behavior

Passing invalid options to :only or :except should raise an error with an informative message.

Actual behavior

Passing invalid options to :only fails quietly and no routes are generated.
Passing invalid arguments to :except succeeds quietly and all default routes are generated (index, create, new, show, update, and destroy).

System configuration

Rails version: Edge

Ruby version: 3.3.0

joshuay03 added a commit to joshuay03/rails that referenced this issue Apr 2, 2024
…ons are given to #resource or #resouces
joshuay03 added a commit to joshuay03/rails that referenced this issue Apr 2, 2024
…ons are given to `#resource` or `#resouces`
joshuay03 added a commit to joshuay03/rails that referenced this issue Apr 2, 2024
…ons are given to `#resource` or `#resouces`
@joshuay03
Copy link
Contributor Author

#51464

joshuay03 added a commit to joshuay03/rails that referenced this issue Apr 2, 2024
…ons are given to `#resource` or `#resouces`
joshuay03 added a commit to joshuay03/rails that referenced this issue Apr 2, 2024
…ons are given to `#resource` or `#resouces`
joshuay03 added a commit to joshuay03/rails that referenced this issue Apr 2, 2024
…ons are given to `#resource` or `#resources`
joshuay03 added a commit to joshuay03/rails that referenced this issue Apr 2, 2024
…ons are given to `#resource` or `#resources`
joshuay03 added a commit to joshuay03/rails that referenced this issue Apr 2, 2024
…ons are given to `#resource` or `#resources`
joshuay03 added a commit to joshuay03/rails that referenced this issue Apr 2, 2024
…ons are given to `#resource` or `#resources`
@joshuay03 joshuay03 changed the title #resource and #resources fail quietly and succeed quietly when invalid arguments are passed to :only end :except options respectively #resource and #resources fail quietly and succeed quietly when invalid arguments are passed to :only and :except options respectively Apr 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant