"users"} 错误" />

没有路由匹配 {:action=>"show", :controller=>"users"} 错误

发布于 2024-12-14 15:49:44 字数 3742 浏览 2 评论 0 原文

我进行了以下操作:

users_controller_spec 中的 rspec 测试:

it "should redirect to the user show page" do
    post :create, :user => @attr
    response.should redirect_to(user_path(assigns(:user)))
end

在我的 users_controller 中,我有以下内容:

def show
  @user = User.find(params[:id])
  @title = @user.name
end

def create
  @title = "Sign up"
  @user = User.new(params[:user])
  if @user.save
    redirect_to @user, :notice => "Signed Up!"
  else
    @title = "Sign up"
    render "new"
  end
end

在我的 paths.rb 中,我有以下内容:

  Psra::Application.routes.draw do
  resources :users
  resources :sessions

  # Root Route

  root :to => 'pages#home'

  # Pages Routes

  match '/contact', :to => 'pages#contact'
  match '/about',   :to => 'pages#about'
  match '/help',    :to => 'pages#help'
  match '/signup',  :to => 'users#new'

  # Users Route

  match '/signup',  :to => 'users#new'

  #Sessions Routes
  get "logout" => "sessions#destroy", :as => "logout"
  get "login" => "sessions#new", :as => "login"

end

这是我的 rake 路线

       users GET    /users(.:format)             {:action=>"index", :controller=>"users"}
             POST   /users(.:format)             {:action=>"create", :controller=>"users"}
    new_user GET    /users/new(.:format)         {:action=>"new", :controller=>"users"}
   edit_user GET    /users/:id/edit(.:format)    {:action=>"edit", :controller=>"users"}
        user GET    /users/:id(.:format)         {:action=>"show", :controller=>"users"}
             PUT    /users/:id(.:format)         {:action=>"update", :controller=>"users"}
             DELETE /users/:id(.:format)         {:action=>"destroy", :controller=>"users"}
    sessions GET    /sessions(.:format)          {:action=>"index", :controller=>"sessions"}
             POST   /sessions(.:format)          {:action=>"create", :controller=>"sessions"}
 new_session GET    /sessions/new(.:format)      {:action=>"new", :controller=>"sessions"}
edit_session GET    /sessions/:id/edit(.:format) {:action=>"edit", :controller=>"sessions"}
     session GET    /sessions/:id(.:format)      {:action=>"show", :controller=>"sessions"}
             PUT    /sessions/:id(.:format)      {:action=>"update", :controller=>"sessions"}
             DELETE /sessions/:id(.:format)      {:action=>"destroy", :controller=>"sessions"}
        root        /                            {:controller=>"pages", :action=>"home"}
     contact        /contact(.:format)           {:controller=>"pages", :action=>"contact"}
       about        /about(.:format)             {:controller=>"pages", :action=>"about"}
        help        /help(.:format)              {:controller=>"pages", :action=>"help"}
      signup        /signup(.:format)            {:controller=>"users", :action=>"new"}
                    /signup(.:format)            {:controller=>"users", :action=>"new"}
      logout GET    /logout(.:format)            {:action=>"destroy", :controller=>"sessions"}
       login GET    /login(.:format)             {:action=>"new", :controller=>"sessions"}

这一切都会导致以下错误:

1) UsersController POST 'create' success should redirect to the user show page
     Failure/Error: response.should redirect_to(user_path(assigns(:user)))
     ActionController::RoutingError:
       No route matches {:action=>"show", :controller=>"users"}
     # ./spec/controllers/users_controller_spec.rb:95:in `block (4 levels) in <top (required)>'

关于我的任何想法我做错了吗?

I have the following going on:

rspec test in users_controller_spec:

it "should redirect to the user show page" do
    post :create, :user => @attr
    response.should redirect_to(user_path(assigns(:user)))
end

In my users_controller I have the following:

def show
  @user = User.find(params[:id])
  @title = @user.name
end

def create
  @title = "Sign up"
  @user = User.new(params[:user])
  if @user.save
    redirect_to @user, :notice => "Signed Up!"
  else
    @title = "Sign up"
    render "new"
  end
end

In my routes.rb I have the following:

  Psra::Application.routes.draw do
  resources :users
  resources :sessions

  # Root Route

  root :to => 'pages#home'

  # Pages Routes

  match '/contact', :to => 'pages#contact'
  match '/about',   :to => 'pages#about'
  match '/help',    :to => 'pages#help'
  match '/signup',  :to => 'users#new'

  # Users Route

  match '/signup',  :to => 'users#new'

  #Sessions Routes
  get "logout" => "sessions#destroy", :as => "logout"
  get "login" => "sessions#new", :as => "login"

end

And Here is my rake routes

       users GET    /users(.:format)             {:action=>"index", :controller=>"users"}
             POST   /users(.:format)             {:action=>"create", :controller=>"users"}
    new_user GET    /users/new(.:format)         {:action=>"new", :controller=>"users"}
   edit_user GET    /users/:id/edit(.:format)    {:action=>"edit", :controller=>"users"}
        user GET    /users/:id(.:format)         {:action=>"show", :controller=>"users"}
             PUT    /users/:id(.:format)         {:action=>"update", :controller=>"users"}
             DELETE /users/:id(.:format)         {:action=>"destroy", :controller=>"users"}
    sessions GET    /sessions(.:format)          {:action=>"index", :controller=>"sessions"}
             POST   /sessions(.:format)          {:action=>"create", :controller=>"sessions"}
 new_session GET    /sessions/new(.:format)      {:action=>"new", :controller=>"sessions"}
edit_session GET    /sessions/:id/edit(.:format) {:action=>"edit", :controller=>"sessions"}
     session GET    /sessions/:id(.:format)      {:action=>"show", :controller=>"sessions"}
             PUT    /sessions/:id(.:format)      {:action=>"update", :controller=>"sessions"}
             DELETE /sessions/:id(.:format)      {:action=>"destroy", :controller=>"sessions"}
        root        /                            {:controller=>"pages", :action=>"home"}
     contact        /contact(.:format)           {:controller=>"pages", :action=>"contact"}
       about        /about(.:format)             {:controller=>"pages", :action=>"about"}
        help        /help(.:format)              {:controller=>"pages", :action=>"help"}
      signup        /signup(.:format)            {:controller=>"users", :action=>"new"}
                    /signup(.:format)            {:controller=>"users", :action=>"new"}
      logout GET    /logout(.:format)            {:action=>"destroy", :controller=>"sessions"}
       login GET    /login(.:format)             {:action=>"new", :controller=>"sessions"}

This all results in the following error:

1) UsersController POST 'create' success should redirect to the user show page
     Failure/Error: response.should redirect_to(user_path(assigns(:user)))
     ActionController::RoutingError:
       No route matches {:action=>"show", :controller=>"users"}
     # ./spec/controllers/users_controller_spec.rb:95:in `block (4 levels) in <top (required)>'

Any ideas on what I'm doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感情洁癖 2024-12-21 15:49:44

在我看来, show 操作没有获取获取正确页面所需的用户信息。 assigns 方法只是创建一个实例变量。 user_path 调用需要一个 User 模拟或对象才能使调用正常工作。

It looks like to me that the show action isn't getting the user information it needs to get the correct page. The assigns method is just creating an instance variable. The user_path call will need a User mock or object to make the call work correctly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文