这是我得到的错误(见下文)
ProjectsControllerTest#test_should_redirect_create_when_not_logged_in:
ActionController::UrlGenerationError: No route matches{:action=>"show", :controller=>"projects"} missing required keys: [:id]
test/controllers/projects_controller_test.rb:11:in `block (2 levels) in <class:ProjectsControllerTest>'
test/controllers/projects_controller_test.rb:10:in `block in <class:ProjectsControllerTest>'
我对Ruby on Rails很陌生,但我一直在环顾四周,尝试不同的事情,没有快乐。我知道项目:id没有发送,但我无法弄清楚为什么
的routes.rb
Rails.application.routes.draw做
get 'password_resets/new'
get 'password_resets/edit'
get 'sessions/new'
get 'users/new'
root 'static_pages#home'
get '/help', to: 'static_pages#help'
get '/about', to: 'static_pages#about'
get '/contact', to: 'static_pages#contact'
get '/signup', to: 'users#new'
get '/login', to: 'sessions#new'
post '/login', to: 'sessions#create'
delete '/logout', to: 'sessions#destroy'
resources :users do
member do
get :following, :followers
end
end
resources :users
resources :account_activation, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
resources :microposts, only: [:create, :destroy]
resources :relationships, only: [:create, :destroy]
resources :projects
end
Project.model.rb
class Project < ApplicationRecord
before_save {name.downcase!}
belongs_to :user
default_scope -> { order(created_at: :desc) }
mount_uploader :picture, PictureUploader
validates :user_id, presence: true
validates :name && :category, presence: true
validates :name, presence: true,
uniqueness: { case_sensitive: false }
Projects_controller.rb
class ProjectsController < ApplicationController
before_action :logged_in_user, only: [:index, :show, :create]
before_action :project_owner, only: :destroy
def index
end
def show
@project = Project.find(params[:id])
end
def new
@project = Project.new
end
def create
@project = current_user.projects.build(project_params)
if @project.save
flash[:success] = "Project Created"
redirect_to @project
else
render 'new'
end
end
def destroy
@project.destroy
flash[:success] = "Project Deleted"
redirect_to request.referrer || root_url
end
private
def project_params
params.require(:project).permit(:name,:category,:picture)
end
def project_owner
@project = current_user.projects.find_by(id: params[:id])
redirect_to root_url if project.nil?
end
end
最后这是我正在运行的测试套装
projects_controller_test.rb
require 'test_helper'
class ProjectsControllerTest < ActionDispatch::IntegrationTest
def setup
@project = projects(:Charity)
end
test "should redirect create when not logged in" do
assert_no_difference 'Project.count' do
post project_path, params: { project: {name: " ",
category: " " } }
end
assert_redirected_to login_url
end
提前致谢!
---编辑1 ----
projects GET /projects(.:format) projects#index
POST /projects(.:format) projects#create
new_project GET /projects/new(.:format) projects#new
edit_project GET /projects/:id/edit(.:format) projects#edit
project GET /projects/:id(.:format) projects#show
PATCH /projects/:id(.:format) projects#update
PUT /projects/:id(.:format) projects#update
DELETE /projects/:id(.:format) projects#destroy
答案 0 :(得分:1)
错误来自这里:
post project_path, params: { project: {name: " ",
category: " " } }
您需要在项目哈希中传递id属性
这是因为您正在使用由需要id的资源生成的标准show route。