Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.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 将资源转换为自定义路由(Ruby/rails)_Ruby On Rails_Rest_Routes - Fatal编程技术网

Ruby on rails 将资源转换为自定义路由(Ruby/rails)

Ruby on rails 将资源转换为自定义路由(Ruby/rails),ruby-on-rails,rest,routes,Ruby On Rails,Rest,Routes,开始在这里构建应用程序。客户端和服务器风格的体系结构跨网络发送活动资源,并存储为activeRecord服务器端。在O Reilly的书中,除了使用scaffold外,还用了一个很好的例子,成功地将它建立起来并运行起来 正在使用rails 2中的map.resources- 我使用的是rails 3,所以它并不真正适用,虽然我确实发布了一个关于从2到3的路线的问题,但我仍然无法转换它 这就是我所看到的: 耙路 resources :user_requests 给出: user_req

开始在这里构建应用程序。客户端和服务器风格的体系结构跨网络发送活动资源,并存储为activeRecord服务器端。在O Reilly的书中,除了使用scaffold外,还用了一个很好的例子,成功地将它建立起来并运行起来

正在使用rails 2中的map.resources- 我使用的是rails 3,所以它并不真正适用,虽然我确实发布了一个关于从2到3的路线的问题,但我仍然无法转换它

这就是我所看到的:

耙路

resources :user_requests
给出:

    user_requests GET    /user_requests(.:format)          {:controller=>"user_requests", :action=>"index"}
                  POST   /user_requests(.:format)          {:controller=>"user_requests", :action=>"create"}
 new_user_request GET    /user_requests/new(.:format)      {:controller=>"user_requests", :action=>"new"}
edit_user_request GET    /user_requests/:id/edit(.:format) {:controller=>"user_requests", :action=>"edit"}
     user_request GET    /user_requests/:id(.:format)      {:controller=>"user_requests", :action=>"show"}
                  PUT    /user_requests/:id(.:format)      {:controller=>"user_requests", :action=>"update"}
                  DELETE /user_requests/:id(.:format)      {:controller=>"user_requests", :action=>"destroy"}
我想删除这个和资源,并有我自己的路线指向我自己的def

这里有一个快速的尝试

  match '/user_requests(.:format)' => 'user_requests#create , :via =>:post' 
  match '/user_requests/:id(.:format)' =>"user_requests#show"
返回与上面几乎完全相同的结果

/user_requests(.:format)          {:controller=>"user_requests", :action=>"create"}
/user_requests/:id(.:format)      {:controller=>"user_requests", :action=>"show"}
除了开头的其他名词和链接。这是一样的,但我自己的路线不工作。 我需要向我的路线添加什么,使它们与资源做相同的事情

我并没有像别人告诉我的那个样保留脚手架,它从未在现实世界中使用过。我将更改我的DEF的名称,但一步一个脚印

服务器显示的错误:

Started POST "/user_requests.xml" for 127.0.0.1 at Tue Jul 12 17:13:32 +0100 2011
  Processing by UserRequestsController#create as XML
  Parameters: {"method"=>"POST", "user_request"=>{"depth"=>3000000, "url"=>"www.stackoverflow.com"}}
  SQL (0.1ms)  SELECT 1 FROM "user_requests" WHERE ("user_requests"."url" = 'www.stackoverflow.com') LIMIT 1
  AREL (0.3ms)  INSERT INTO "user_requests" ("updated_at", "depth", "url", "created_at") VALUES ('2011-07-12 16:13:32.765392', 3000000, 'www.stackoverflow.com', '2011-07-12 16:13:32.765392')
Completed 404 Not Found in 17ms

ActionController::RoutingError (No route matches {:controller=>"user_requests", :id=>#<UserRequest id: 6, url: "www.stackoverflow.com", depth: 3000000, created_at: "2011-07-12 16:13:32", updated_at: "2011-07-12 16:13:32">, :action=>"show"}):
  app/controllers/user_requests_controller.rb:19:in `create'
  app/controllers/user_requests_controller.rb:16:in `create'

Rendered /Library/Ruby/Gems/1.8/gems/actionpack-3.0.9/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (0.8ms)

您的UserRequestsController的第19行可能有如下内容

redirect_to @user_request
它尝试猜测显示UserRequest对象的URL。Rails不再知道如何做到这一点;您已经丢失了由资源(如用户请求路径、新用户请求路径等)生成的助手方法

您可以告诉Rails生成show helper方法,方法是在show route中添加:as选项,而不使用_path后缀:


现在,您可以访问用户请求路径和用户请求url,Rails可以使用这些路径来查找显示用户请求的url。

在阅读了一些动词约束后,添加了“:via:post”,因此这里有一个疯狂的事情。它在添加了:via=>:“post”之后更新了数据库,但仍然给我返回了一个错误确实有一个位置标记而不是重定向到。我没有尝试将该位置作为对web应用程序而不是浏览器的响应,而是将其删除,从而解决了问题。我会进一步研究这个问题。非常感谢。
 match '/user_requests/:id(.:format)' => "user_requests#show", :as => 'user_request'