Rails 5重命名单个资源路由名称

时间:2016-10-27 10:03:52

标签: ruby-on-rails ruby routing routes

我正在尝试创建一个单独的:show route,以在:id参数上使用路由通配。为此,我创建了一个没有show route的资源路由,也创建了一个单独的show route:

resource :test, except: [:show]
get 'test/*id', to: 'test#show', as: :test

问题是我收到错误:You may have defined two routes with the same name using theoption, or you may be overriding a route already defined by a resource with the same naming.

如果我删除as: :test则可行。 rails routes显示:

    tests POST   /tests(.:format)
 new_test GET    /tests/new(.:format)
edit_test GET    /tests/:id/edit(.:format)
     test PATCH  /tests/:id(.:format)     <-- WHY??
          DELETE /tests/:id(.:format)
          GET    /tests/*id(.:format)

如您所见,resourcesPATCH路线重命名为:test。如果我删除该路由,DELETE路由的名称为test,依此类推。如何阻止resources专门使用test路由名称?我无法在resource区块上方移动我的通道路线,因为那时所有其他路线也是全球化的。

我想要的是什么:

    tests POST   /tests(.:format)
 new_test GET    /tests/new(.:format)
edit_test GET    /tests/:id/edit(.:format)
          PATCH  /tests/:id(.:format)
          DELETE /tests/:id(.:format)
     test GET    /tests/*id(.:format)

3 个答案:

答案 0 :(得分:1)

Rails对所有这四条路由[show(GET),update(PUT / PATCH),destroy(DELETE)]使用相同的前缀(例如,在您的情况下为“test”),并且它使用HTTP识别不同的路由动词。

答案 1 :(得分:0)

我不明白你的问题,但如果你看看Rails指南“奇异资源”,你可以看到:

SELECT lastname COLLATE latin1_general_ci_ai      AS 'LastName',
       MIN(lastname COLLATE latin1_general_ci_as) AS 'CorrectLastName',
       MAX(lastname COLLATE latin1_general_ci_as) AS 'IncorrectLastName'
INTO   #lastname
FROM   learner WITH (nolock)
GROUP  BY lastname COLLATE latin1_general_ci_ai
HAVING MIN(lastname COLLATE latin1_general_ci_as) <> MAX(lastname COLLATE latin1_general_ci_as);

IF Object_id ('tempdb..#LastNameUpdate') IS NOT NULL
  DROP TABLE #lastnameupdate

SELECT L.learnerid,
       L.lastname,
       LN.correctlastname,
       LN.incorrectlastname
INTO   #lastnameupdate
FROM   learner L WITH (nolock)
       INNER JOIN #lastname AS LN
               ON L.lastname = LN.incorrectlastname

UPDATE L
SET    L.lastname = LNU.correctlastname
FROM   learner L
       INNER JOIN #lastnameupdate AS LNU
               ON L.learnerid = LNU.learnerid  

show,create,update和destroy使用相同的路由,但使用不同的HTTP谓词。在你的情况下,DECLARE @column1 varchar(max) = 'YYYY:MM:DD HH:MM:SS' SELECT REPLACE(SUBSTRING(@column1, 1, CHARINDEX(' ', @column1) - 1), ':' , '-') + ' ' + SUBSTRING(@column1, CHARINDEX(' ', @column1) + 1, 8000) 用PATCH动词编写,因为表中的动词,空名称意味着它使用与上面相同的名称。

答案 2 :(得分:0)

首先,

 test PATCH  /tests/:id(.:format)     <-- WHY??
      DELETE /tests/:id(.:format)
      GET    /tests/*id(.:format)
  1. Patch适用于您的Update方法路线。
  2. 删除是针对destroy方法路由。
  3. get 'test/*id', to: 'test#show', as: :test
  4. 生成的自定义路由添加

    所以,在这里你可以使用不同的别名制作你的节目路线。就像使用as :show_test

    一样