Ruby on rails Rails 3重命名路由
我刚刚开始使用Rails3,我不太明白如何重命名路由 我想要什么: 要将路径重命名为Ruby on rails Rails 3重命名路由,ruby-on-rails,ruby-on-rails-3,routing,rename,Ruby On Rails,Ruby On Rails 3,Routing,Rename,我刚刚开始使用Rails3,我不太明白如何重命名路由 我想要什么: 要将路径重命名为users#showcontroller/action对。因此,URL不是www.example.com/users/show/1,而是www.example.com/1/home 将来,我还希望能够在终端上添加其他路径,例如: www.example.com/1/home/profile/ 如何设置我的用户资源: resources :users, :except => [:destroy] do r
users#show
controller/action对。因此,URL不是www.example.com/users/show/1
,而是www.example.com/1/home
将来,我还希望能够在终端上添加其他路径,例如:
www.example.com/1/home/profile/
如何设置我的用户资源:
resources :users, :except => [:destroy] do
resources :favorites, :only => [:show, :update]
resources :profiles, :only => [:show, :update]
end
match :home, :to => 'users#show'
ActiveRecord::RecordNotFound in UsersController#show
Couldn't find User without an ID
Started GET "/home" for 127.0.0.1 at 2011-03-10 13:36:15 -0500
Processing by UsersController#show as HTML
[1m[35mUser Load (1.6ms)[0m SELECT "users".* FROM "users" WHERE ("users"."id" = 101) LIMIT 1
Completed in 192ms
ActiveRecord::RecordNotFound (Couldn't find User without an ID):
app/controllers/users_controller.rb:19:in `show'
def show
@user = User.find(params[:id])
respond_to do |format|
format.html # show.html.haml
end
end
我的尝试:
resources :users, :except => [:destroy] do
resources :favorites, :only => [:show, :update]
resources :profiles, :only => [:show, :update]
end
match :home, :to => 'users#show'
ActiveRecord::RecordNotFound in UsersController#show
Couldn't find User without an ID
Started GET "/home" for 127.0.0.1 at 2011-03-10 13:36:15 -0500
Processing by UsersController#show as HTML
[1m[35mUser Load (1.6ms)[0m SELECT "users".* FROM "users" WHERE ("users"."id" = 101) LIMIT 1
Completed in 192ms
ActiveRecord::RecordNotFound (Couldn't find User without an ID):
app/controllers/users_controller.rb:19:in `show'
def show
@user = User.find(params[:id])
respond_to do |format|
format.html # show.html.haml
end
end
发生了什么:
resources :users, :except => [:destroy] do
resources :favorites, :only => [:show, :update]
resources :profiles, :only => [:show, :update]
end
match :home, :to => 'users#show'
ActiveRecord::RecordNotFound in UsersController#show
Couldn't find User without an ID
Started GET "/home" for 127.0.0.1 at 2011-03-10 13:36:15 -0500
Processing by UsersController#show as HTML
[1m[35mUser Load (1.6ms)[0m SELECT "users".* FROM "users" WHERE ("users"."id" = 101) LIMIT 1
Completed in 192ms
ActiveRecord::RecordNotFound (Couldn't find User without an ID):
app/controllers/users_controller.rb:19:in `show'
def show
@user = User.find(params[:id])
respond_to do |format|
format.html # show.html.haml
end
end
development.log文件中有什么内容:
resources :users, :except => [:destroy] do
resources :favorites, :only => [:show, :update]
resources :profiles, :only => [:show, :update]
end
match :home, :to => 'users#show'
ActiveRecord::RecordNotFound in UsersController#show
Couldn't find User without an ID
Started GET "/home" for 127.0.0.1 at 2011-03-10 13:36:15 -0500
Processing by UsersController#show as HTML
[1m[35mUser Load (1.6ms)[0m SELECT "users".* FROM "users" WHERE ("users"."id" = 101) LIMIT 1
Completed in 192ms
ActiveRecord::RecordNotFound (Couldn't find User without an ID):
app/controllers/users_controller.rb:19:in `show'
def show
@user = User.find(params[:id])
respond_to do |format|
format.html # show.html.haml
end
end
用户控制器中有什么:
resources :users, :except => [:destroy] do
resources :favorites, :only => [:show, :update]
resources :profiles, :only => [:show, :update]
end
match :home, :to => 'users#show'
ActiveRecord::RecordNotFound in UsersController#show
Couldn't find User without an ID
Started GET "/home" for 127.0.0.1 at 2011-03-10 13:36:15 -0500
Processing by UsersController#show as HTML
[1m[35mUser Load (1.6ms)[0m SELECT "users".* FROM "users" WHERE ("users"."id" = 101) LIMIT 1
Completed in 192ms
ActiveRecord::RecordNotFound (Couldn't find User without an ID):
app/controllers/users_controller.rb:19:in `show'
def show
@user = User.find(params[:id])
respond_to do |format|
format.html # show.html.haml
end
end
因此,很明显,它正在存储用户id,如开发日志中所示为101
,但出于何种原因,我仍然会收到这个错误
非常感谢您提供的任何帮助 我无法解释它为什么发出SQL请求,但它没有使用101来查找用户。如果是,则会出现以下错误:
ActiveRecord::RecordNotFound: Couldn't find User with ID=101
因为它说无法找到没有ID的用户
,所以它可能正在调用User.find(nil)
无论如何,我们在应用程序中也做了一些类似的事情,除了名称而不是ID。它们在routes文件的底部匹配,如下所示:
match '/:current_region' => 'offers#show', :as => 'region_home'
然后在控制器中,可以从参数params[:current_region]
加载模型:
def load_region
@current_region = Region.find_by_slug(params[:current_region] || cookies[:current_region])
end
在许多操作之前,我们将其用作过滤器,因此,我们像这样定义它,而不是在show
操作中显式调用它:
class OffersController < ActionController::Base
before_filter :load_region
def show
# do stuff with @current_region here
end
end
类OffersController
您只需将
:current_region
更改为:id
您应该在匹配中提供段键:
match ':id/home' => 'users#show'
但是通过这种重命名,您将不会得到RESTful路由
另一件事是用户配置文件。如果一个用户只能有一个配置文件,最好声明单个资源路由:
resources :users do
resource :profile
end
这会产生一个路由错误:
没有路由匹配{:controller=>“users”,:action=>“show”}
它看起来像是在用户控制器的new
操作中查找show
操作ActionController::RoutingError(没有路由匹配{:controller=>“users”,:action=>“show”}):app/controllers/users\u controller.rb:33:在“new”(新建)
中,您的root:to=>
有问题。首先,将其放在路线的底部。如果没有帮助,请显示您的路线。rb。很抱歉,作为Rails的新手,查看您的代码会让我更加困惑。如果你能告诉我如何使用我的代码,我会更容易理解。