Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/53.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails routes.rb意外行为_Ruby On Rails_Routes_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails Rails routes.rb意外行为

Ruby on rails Rails routes.rb意外行为,ruby-on-rails,routes,ruby-on-rails-4,Ruby On Rails,Routes,Ruby On Rails 4,编辑我的Rails 4 app routes.rb文件时,我会遇到意想不到的行为(无论如何,从我的角度来看,这是意想不到的) 我正在尝试创建更新预订记录的链接。我在BookingsController中创建了一个名为“撤消准备”的操作,用于处理更新过程。我希望链接传递预订id,我的链接代码如下: <%= link_to "Withdraw this booking", bookings_withdraw_path(@booking), :confirm => "Are you sur

编辑我的Rails 4 app routes.rb文件时,我会遇到意想不到的行为(无论如何,从我的角度来看,这是意想不到的)

我正在尝试创建更新预订记录的链接。我在BookingsController中创建了一个名为“撤消准备”的操作,用于处理更新过程。我希望链接传递预订id,我的链接代码如下:

<%= link_to "Withdraw this booking", bookings_withdraw_path(@booking), :confirm => "Are you sure you want to withdraw this booking?", :method => :patch %>
然后,当我运行rake命令检查路由时,它显示如下:

bookings_withdrawn GET    /bookings/withdrawn(.:format)          bookings#withdrawn
                   PATCH  /bookings/withdraw/:bid(.:format)      bookings#withdraw
如您所见,撤回路径是上述路径的一部分(顺便说一句,撤回是另一条路径)。如果我从路径中删除/:bid部分,那么它将创建自己的路径,这正是我所期望的

有人能解释为什么会这样吗?

试试这个

在您的routes文件中,将一个块传递给
resources:bookings
,如下所示

  resources :bookings do
    member do
      patch :withdraw
    end
  end
把这个拿走

match 'bookings/withdraw/:bid' => 'bookings#withdraw', via: 'patch'

正如我在评论中所写,您应该在路线中添加
:As
选项,即:

match 'bookings/withdraw/:bid' => 'bookings#withdraw', via: 'patch', as: 'bookings_withdraw'

命名路由可能不是自动生成的,因为动态部分
:bid
,在这种情况下,AFAIK Rails不会生成隐式命名路由,因此您必须显式添加它们,但我仍然无法在文档中找到它,如果有人已经拥有并可以共享,那么我将更新我的答案。

我现在在任何源代码中都找不到它,但我很确定动态路线在默认情况下不会命名,您必须通过将
添加为:'bookings\u draw'
(在这种情况下)来明确指定它,以便它是预期的行为。这就是它-谢谢。我没有意识到这一点。非常感谢!如果你想添加它作为一个答案,我会为你标记它正确。
match 'bookings/withdraw/:bid' => 'bookings#withdraw', via: 'patch', as: 'bookings_withdraw'