{"action":"created","issue":{"url":"https://api.github.com/repos/phoenixframework/phoenix/issues/1738","repository_url":"https://api.github.com/repos/phoenixframework/phoenix","labels_url":"https://api.github.com/repos/phoenixframework/phoenix/issues/1738/labels{/name}","comments_url":"https://api.github.com/repos/phoenixframework/phoenix/issues/1738/comments","events_url":"https://api.github.com/repos/phoenixframework/phoenix/issues/1738/events","html_url":"https://github.com/phoenixframework/phoenix/issues/1738","id":157887068,"number":1738,"title":"wrong controller function used when trying to create new resource when there are multiple scopes with same path but no duplicate routes","user":{"login":"andshape","id":19300869,"avatar_url":"https://avatars.githubusercontent.com/u/19300869?v=3","gravatar_id":"","url":"https://api.github.com/users/andshape","html_url":"https://github.com/andshape","followers_url":"https://api.github.com/users/andshape/followers","following_url":"https://api.github.com/users/andshape/following{/other_user}","gists_url":"https://api.github.com/users/andshape/gists{/gist_id}","starred_url":"https://api.github.com/users/andshape/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/andshape/subscriptions","organizations_url":"https://api.github.com/users/andshape/orgs","repos_url":"https://api.github.com/users/andshape/repos","events_url":"https://api.github.com/users/andshape/events{/privacy}","received_events_url":"https://api.github.com/users/andshape/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2016-06-01T11:46:45Z","updated_at":"2016-06-01T11:54:52Z","closed_at":"2016-06-01T11:54:52Z","body":"### Precheck \r\nLooked at SO, Phoenix mailing list and existing bug list. The closest related question I could find on SO is this: http://stackoverflow.com/questions/36170123/phoenix-elixir-ecto-casterror-value-new-in-where-cannot-be-cast-to-type\r\n\r\n### Environment\r\n\r\n* Elixir version (elixir -v): 1.2.4\r\n* Phoenix version (mix deps): \r\n\r\n1. connection 1.0.2 (Hex package) (mix)\r\n2.  fs 0.9.1 (Hex package) (rebar)\r\n3.  gettext 0.11.0 (Hex package) (mix)\r\n4.  ranch 1.2.1 (Hex package) (rebar)\r\n5.  poolboy 1.5.1 (Hex package) (rebar)\r\n6.  decimal 1.1.2 (Hex package) (mix)\r\n7.  poison 2.1.0 (Hex package) (mix)\r\n8.  db_connection 0.2.5 (Hex package) (mix)\r\n9.  cowlib 1.0.2 (Hex package) (rebar)\r\n10.  cowboy 1.0.4 (Hex package) (rebar)\r\n11.  plug 1.1.5 (Hex package) (mix)\r\n12.  phoenix_html 2.5.1 (Hex package) (mix)\r\n13.  phoenix 1.1.4 (Hex package) (mix)\r\n14.  phoenix_live_reload 1.0.5 (Hex package) (mix)\r\n15.  postgrex 0.11.1 (Hex package) (mix)\r\n16.  ecto 1.1.8 (Hex package) (mix)\r\n17.  phoenix_ecto 2.0.2 (Hex package) (mix)\r\n\r\n* NodeJS version (node -v): v5.10.1\r\n* NPM version (npm -v): 3.8.3\r\n* Operating system: Windows 10\r\n\r\n\r\n### Setup\r\nSteps to reproduce:\r\n\r\n1. Create a new phoenix project\r\n2. run: Mix phoenix.gen.html User users name:string\r\n3. Define your routes like this:\r\n\r\n```\r\nscope \"/\", Routestest do\r\n    pipe_through :browser # Use the default browser stack\r\n\r\n    resources \"/users\", UserController, only: [:show]\r\n  end\r\n\r\n  scope \"/\", Routestest do\r\n    pipe_through :browser # Use the default browser stack\r\n\r\n    get \"/\", PageController, :index\r\n\r\n    resources \"/users\", UserController, only: [:create, :new, :index, :edit, :update, :delete]\r\n  end\r\n```\r\nNote how there are two scopes with the same path but with no overlapping routes, which should be legal according to this http://www.phoenixframework.org/docs/routing. \r\n4. run mix ecto.setup\r\n5. run mix phoenix.server \r\n6. navigate to http://localhost:4000/users \r\n7. click on New user link.\r\n\r\nNote that the link is autogenerated (from phoenix.gen.html): <%= link \"New user\", to: user_path(@conn, :new) %>\r\n\r\n### Expected behavior\r\nClicking on the \"New user\" link should lead me to the form where I can create the new user.\r\n\r\n### Actual behavior\r\nCrash: Phoenix routes me to the \"show\" function instead of the \"new\" function (and sets id = new)\r\n\r\n\r\nThe problem seem to be that it looks at the very first scope and only sees the show action and doesn't take into account that a new action is defined in a later scope. This means that the routes are defined as:\r\n```\r\n\r\nuser_path  GET     /users/:id       Routestest.UserController :show\r\npage_path  GET     /                Routestest.PageController :index\r\nuser_path  GET     /users           Routestest.UserController :index\r\nuser_path  GET     /users/:id/edit  Routestest.UserController :edit\r\nuser_path  GET     /users/new       Routestest.UserController :new\r\n```\r\n\r\nThis means that the route http://localhost:4000/users/new matches the show route (id = new). If I, on the other hand, had only defined one scope for the users resource and had explicitly defined the allowed actions as [:show, :create, :new, :index, :edit, :update, :delete] it would have been smart enough to know that :new takes \"precedence\" to show (even though it is defined later in the action list). This is also reflected in the routes if you run mix phoenix.routes in this case:\r\n\r\n```\r\nuser_path  GET     /users           Routestest.UserController :index\r\nuser_path  GET     /users/:id/edit  Routestest.UserController :edit\r\nuser_path  GET     /users/new       Routestest.UserController :new\r\nuser_path  GET     /users/:id       Routestest.UserController :show\r\n```\r\n\r\nYou see the new-route is higher up the list than the show-route. I would expect it to be smart enough to do the same if the actions had been defined in different scopes with the same path."},"comment":{"url":"https://api.github.com/repos/phoenixframework/phoenix/issues/comments/222970435","html_url":"https://github.com/phoenixframework/phoenix/issues/1738#issuecomment-222970435","issue_url":"https://api.github.com/repos/phoenixframework/phoenix/issues/1738","id":222970435,"user":{"login":"chrismccord","id":576796,"avatar_url":"https://avatars.githubusercontent.com/u/576796?v=3","gravatar_id":"","url":"https://api.github.com/users/chrismccord","html_url":"https://github.com/chrismccord","followers_url":"https://api.github.com/users/chrismccord/followers","following_url":"https://api.github.com/users/chrismccord/following{/other_user}","gists_url":"https://api.github.com/users/chrismccord/gists{/gist_id}","starred_url":"https://api.github.com/users/chrismccord/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/chrismccord/subscriptions","organizations_url":"https://api.github.com/users/chrismccord/orgs","repos_url":"https://api.github.com/users/chrismccord/repos","events_url":"https://api.github.com/users/chrismccord/events{/privacy}","received_events_url":"https://api.github.com/users/chrismccord/received_events","type":"User","site_admin":false},"created_at":"2016-06-01T11:54:52Z","updated_at":"2016-06-01T11:54:52Z","body":"This is expected, /users/new is the same matching path as /users/123, but when you used `resources \"/users\"`... phoenix will define the routes in the proper order that \"/users/new\" matches first. Because you used the first scope/route to specifically handle `:show`, it will always match /users/new, regardless of the 2nd route.\r\n\r\n> The problem seem to be that it looks at the very first scope and only sees the show action and doesn't take into account that a new action is defined in a later scope.\r\n\r\nThis isn't something we handled for you so you need to handle the route precedence yourself and if you have overlapping scopes you need to take care in the order they are defined in. Hope that helps!"}}
{"actor":{"display_login":"chrismccord"}}