新手问题 路由表的问题?

paul1113 · 2014年03月17日 · 最后由 glz1992 回复于 2014年03月17日 · 4111 次阅读

liuchao@liuchao:~/rails_projects/sample_app$ rails s => Booting WEBrick => Rails 4.1.0.beta1 application starting in development on http://0.0.0.0:3000 => Run rails server -h for more startup options => Notice: server is listening on all interfaces (0.0.0.0). Consider using 127.0.0.1 (--binding option) => Ctrl-C to shutdown server Exiting /home/liuchao/.rvm/gems/ruby-2.1.0/gems/actionpack-4.1.0.beta1/lib/action_dispatch/routing/mapper.rb:238:in `default_controller_and_action': Missing :action key on routes definition, please check your routes. (ArgumentError) 以上是我启动服务器时候的报错,以下是我的 route.rb SampleApp::Application.routes.draw do

get "/users/new" get "/no_test_framework"

root to: 'static_pages#home' match '/signup', to: 'users#new', via: 'get' match '/help', to: 'static_pages#help', via: 'get' match '/about', to: 'static_pages#about', via: 'get' match '/contact', to: 'static_pages#contact', via: 'get'

# The priority is based upon order of creation: first created -> highest priority. # See how all your routes lay out with "rake routes".

# You can have the root of your site routed with "root" # root 'static_pages#home'

# Example of regular route: # get 'products/:id' => 'catalog#view'

# Example of named route that can be invoked with purchase_url(id: product.id) # get 'products/:id/purchase' => 'catalog#purchase', as: :purchase

# Example resource route (maps HTTP verbs to controller actions automatically): # resources :products

# Example resource route with options: # resources :products do # member do # get 'short' # post 'toggle' # end # # collection do # get 'sold' # end # end

# Example resource route with sub-resources: # resources :products do # resources :comments, :sales # resource :seller # end

# Example resource route with more complex sub-resources: # resources :products do # resources :comments # resources :sales do # get 'recent', on: :collection # end # end

# Example resource route with concerns: # concern :toggleable do # post 'toggle' # end # resources :posts, concerns: :toggleable # resources :photos, concerns: :toggleable

# Example resource route within a namespace: # namespace :admin do # # Directs /admin/products/* to Admin::ProductsController # # (app/controllers/admin/products_controller.rb) # resources :products # end end

Failures:

1) UsersController GET '–no-test-framework' returns http success Failure/Error: get '–no-test-framework' ActionController::UrlGenerationError: No route matches {:action=>"–no-test-framework", :controller=>"users"} # ./spec/controllers/users_controller_spec.rb:14:in `block (3 levels) in '

Finished in 0.28438 seconds 18 examples, 1 failure, 3 pending

Failed examples:

rspec ./spec/controllers/users_controller_spec.rb:13 # UsersController GET '–no-test-framework' returns http success

no_test_framework No route matches {:action=>"–no-test-framework", :controller=>"users"} 请比较这两个有什么不同?/

#2 楼 @paul1113 下划线和--分不清楚

结果都是失败的!

贴出你的测试代码和 no_test_framework 对应 action 代码,看看

···## assumes 5 possible vote categories, but easily adaptable

DEFAULT_PRIOR = [2, 2, 2, 2, 2]

input is a five-element array of integers

output is a score between 1.0 and 5.0

def score votes, prior=DEFAULT_PRIOR posterior = votes.zip(prior).map { |a, b| a + b } sum = posterior.inject { |a, b| a + b } posterior. map.with_index { |v, i| (i + 1) * v }. inject { |a, b| a + b }. to_f / sum end···

求这段代码什么意思,不是搞 ruby,被限制发帖的路过。

class UsersController < ApplicationController def new end def no_test_framework end end

看哪个文件的测试代码@floger

@paul1113 ./spec/controllers/users_controller_spec.rb:13 # UsersController GET '–no-test-framework'

路由这么写

get "/users/no_test_framework"

或者这么写

match '/no_test_framework', to: 'users#no_test_framework', via: 'get' 

谢谢 floger 我试一试

不行的 估计我运行 rails generate controller Users new --no-test-framework 这条命令的时候出了什么问题

@paul1113 --no-test-framework 这个命令告送你不使用 test::unit 的测试框架。这个是在 rails new 项目时使用的,不是 generate 时使用的。

贴代码要用 markdown 格式化一下。

#6 楼 @ggaaooppeenngg 你在 ruby 官网里面把这几个函数看懂了就差不多了。zip map inject with_index

需要 登录 后方可回复, 如果你还没有账号请 注册新账号