我如何获得以下格式的 url
how do I have an url in the below format
我能够在 rails generate controller static_pages about
的帮助下生成 url 和关联的网页。所以 url 会像这样 www.bla.com/static_pages/about'. Now I want to generate this url
bla.com/help` 不使用生成,
我在 routes.rb 中添加了这样一条路线
get 'help'
然后我在 application_controller.rb 中添加了一个像这样的动作
def hello
render text: "Hello"
end
然后当启动服务器时,我得到以下错误
Exiting
/usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.2/lib/action_dispatch/routing/mapper.rb:275:in `check_part': Missing :controller key on routes definition, please check your routes. (ArgumentError)
from /usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.2/lib/action_dispatch/routing/mapper.rb:255:in `check_controller_and_action'
from /usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.2/lib/action_dispatch/routing/mapper.rb:178:in `normalize_options!'
from /usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.2/lib/action_dispatch/routing/mapper.rb:100:in `initialize'
from /usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.2/lib/action_dispatch/routing/mapper.rb:78:in `new'
from /usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.2/lib/action_dispatch/routing/mapper.rb:78:in `build'
from /usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.2/lib/action_dispatch/routing/mapper.rb:1559:in `add_route'
from /usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.2/lib/action_dispatch/routing/mapper.rb:1536:in `decomposed_match'
from /usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.2/lib/action_dispatch/routing/mapper.rb:1517:in `block in match'
from /usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.2/lib/action_dispatch/routing/mapper.rb:1507:in `each'
from /usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.2/lib/action_dispatch/routing/mapper.rb:1507:in `match'
from /usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.2/lib/action_dispatch/routing/mapper.rb:689:in `map_method'
from /usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.2/lib/action_dispatch/routing/mapper.rb:650:in `get'
from /home/ubuntu/workspace/sample_app/config/routes.rb:12:in `block in <top (required)>'
from /usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.2/lib/action_dispatch/routing/route_set.rb:432:in `instance_exec'
from /usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.2/lib/action_dispatch/routing/route_set.rb:432:in `eval_block'
from /usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.2/lib/action_dispatch/routing/route_set.rb:410:in `draw'
from /home/ubuntu/workspace/sample_app/config/routes.rb:1:in `<top (required)>'
from /usr/local/rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:268:in `load'
from /usr/local/rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:268:in `block in load'
from /usr/local/rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:240:in `load_dependency'
from /usr/local/rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:268:in `load'
from /usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/application/routes_reloader.rb:40:in `block in load_paths'
from /usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/application/routes_reloader.rb:40:in `each'
from /usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/application/routes_reloader.rb:40:in `load_paths'
from /usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/application/routes_reloader.rb:16:in `reload!'
from /usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/application/routes_reloader.rb:26:in `block in updater'
from /usr/local/rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/file_update_checker.rb:75:in `call'
from /usr/local/rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/file_update_checker.rb:75:in `execute'
from /usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/application/routes_reloader.rb:27:in `updater'
from /usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/application/routes_reloader.rb:7:in `execute_if_updated'
from /usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/application/finisher.rb:69:in `block in <module:Finisher>'
from /usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/initializable.rb:30:in `instance_exec'
from /usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/initializable.rb:30:in `run'
from /usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/initializable.rb:55:in `block in run_initializers'
from /usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:226:in `block in tsort_each'
from /usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:348:in `block (2 levels) in each_strongly_connected_component'
from /usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:429:in `each_strongly_connected_component_from'
from /usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:347:in `block in each_strongly_connected_component'
from /usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:345:in `each'
from /usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:345:in `call'
from /usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:345:in `each_strongly_connected_component'
from /usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:224:in `tsort_each'
from /usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:203:in `tsort_each'
from /usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/initializable.rb:54:in `run_initializers'
from /usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/application.rb:352:in `initialize!'
from /home/ubuntu/workspace/sample_app/config/environment.rb:5:in `<top (required)>'
from /home/ubuntu/workspace/sample_app/config.ru:3:in `require'
from /home/ubuntu/workspace/sample_app/config.ru:3:in `block in <main>'
from /usr/local/rvm/gems/ruby-2.2.1/gems/rack-1.6.4/lib/rack/builder.rb:55:in `instance_eval'
from /usr/local/rvm/gems/ruby-2.2.1/gems/rack-1.6.4/lib/rack/builder.rb:55:in `initialize'
from /home/ubuntu/workspace/sample_app/config.ru:in `new'
from /home/ubuntu/workspace/sample_app/config.ru:in `<main>'
from /usr/local/rvm/gems/ruby-2.2.1/gems/rack-1.6.4/lib/rack/builder.rb:49:in `eval'
from /usr/local/rvm/gems/ruby-2.2.1/gems/rack-1.6.4/lib/rack/builder.rb:49:in `new_from_string'
from /usr/local/rvm/gems/ruby-2.2.1/gems/rack-1.6.4/lib/rack/builder.rb:40:in `parse_file'
from /usr/local/rvm/gems/ruby-2.2.1/gems/rack-1.6.4/lib/rack/server.rb:299:in `build_app_and_options_from_config'
from /usr/local/rvm/gems/ruby-2.2.1/gems/rack-1.6.4/lib/rack/server.rb:208:in `app'
from /usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/commands/server.rb:61:in `app'
from /usr/local/rvm/gems/ruby-2.2.1/gems/rack-1.6.4/lib/rack/server.rb:336:in `wrapped_app'
from /usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/commands/server.rb:139:in `log_to_stdout'
from /usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/commands/server.rb:78:in `start'
from /usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/commands/commands_tasks.rb:80:in `block in server'
from /usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/commands/commands_tasks.rb:75:in `tap'
from /usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/commands/commands_tasks.rb:75:in `server'
from /usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/commands.rb:17:in `<top (required)>'
from /home/ubuntu/workspace/sample_app/bin/rails:8:in `require'
from /home/ubuntu/workspace/sample_app/bin/rails:8:in `<top (required)>'
from /usr/local/rvm/gems/ruby-2.2.1/gems/spring-1.1.3/lib/spring/client/rails.rb:27:in `load'
from /usr/local/rvm/gems/ruby-2.2.1/gems/spring-1.1.3/lib/spring/client/rails.rb:27:in `call'
from /usr/local/rvm/gems/ruby-2.2.1/gems/spring-1.1.3/lib/spring/client/command.rb:7:in `call'
from /usr/local/rvm/gems/ruby-2.2.1/gems/spring-1.1.3/lib/spring/client.rb:26:in `run'
from /usr/local/rvm/gems/ruby-2.2.1/gems/spring-1.1.3/bin/spring:48:in `<top (required)>'
from /usr/local/rvm/gems/ruby-2.2.1/gems/spring-1.1.3/lib/spring/binstub.rb:11:in `load'
from /usr/local/rvm/gems/ruby-2.2.1/gems/spring-1.1.3/lib/spring/binstub.rb:11:in `<top (required)>'
from /home/ubuntu/workspace/sample_app/bin/spring:13:in `require'
from /home/ubuntu/workspace/sample_app/bin/spring:13:in `<top (required)>'
from bin/rails:3:in `load'
from bin/rails:3:in `<main>'
Missing :controller key on routes definition, please check your
routes. (ArgumentError)
如错误所述,您需要指定 controller
和 action
匹配 route
。以下应该有效。
get "help", to: 'static_pages#about', as: 'help'
^^^^^^ ^^^^
controller action
此外,我建议您遵循这些 Guides 以清楚地了解如何定义路由。
我能够在 rails generate controller static_pages about
的帮助下生成 url 和关联的网页。所以 url 会像这样 www.bla.com/static_pages/about'. Now I want to generate this url
bla.com/help` 不使用生成,
我在 routes.rb 中添加了这样一条路线
get 'help'
然后我在 application_controller.rb 中添加了一个像这样的动作
def hello
render text: "Hello"
end
然后当启动服务器时,我得到以下错误
Exiting
/usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.2/lib/action_dispatch/routing/mapper.rb:275:in `check_part': Missing :controller key on routes definition, please check your routes. (ArgumentError)
from /usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.2/lib/action_dispatch/routing/mapper.rb:255:in `check_controller_and_action'
from /usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.2/lib/action_dispatch/routing/mapper.rb:178:in `normalize_options!'
from /usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.2/lib/action_dispatch/routing/mapper.rb:100:in `initialize'
from /usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.2/lib/action_dispatch/routing/mapper.rb:78:in `new'
from /usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.2/lib/action_dispatch/routing/mapper.rb:78:in `build'
from /usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.2/lib/action_dispatch/routing/mapper.rb:1559:in `add_route'
from /usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.2/lib/action_dispatch/routing/mapper.rb:1536:in `decomposed_match'
from /usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.2/lib/action_dispatch/routing/mapper.rb:1517:in `block in match'
from /usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.2/lib/action_dispatch/routing/mapper.rb:1507:in `each'
from /usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.2/lib/action_dispatch/routing/mapper.rb:1507:in `match'
from /usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.2/lib/action_dispatch/routing/mapper.rb:689:in `map_method'
from /usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.2/lib/action_dispatch/routing/mapper.rb:650:in `get'
from /home/ubuntu/workspace/sample_app/config/routes.rb:12:in `block in <top (required)>'
from /usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.2/lib/action_dispatch/routing/route_set.rb:432:in `instance_exec'
from /usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.2/lib/action_dispatch/routing/route_set.rb:432:in `eval_block'
from /usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.2/lib/action_dispatch/routing/route_set.rb:410:in `draw'
from /home/ubuntu/workspace/sample_app/config/routes.rb:1:in `<top (required)>'
from /usr/local/rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:268:in `load'
from /usr/local/rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:268:in `block in load'
from /usr/local/rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:240:in `load_dependency'
from /usr/local/rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:268:in `load'
from /usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/application/routes_reloader.rb:40:in `block in load_paths'
from /usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/application/routes_reloader.rb:40:in `each'
from /usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/application/routes_reloader.rb:40:in `load_paths'
from /usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/application/routes_reloader.rb:16:in `reload!'
from /usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/application/routes_reloader.rb:26:in `block in updater'
from /usr/local/rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/file_update_checker.rb:75:in `call'
from /usr/local/rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/file_update_checker.rb:75:in `execute'
from /usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/application/routes_reloader.rb:27:in `updater'
from /usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/application/routes_reloader.rb:7:in `execute_if_updated'
from /usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/application/finisher.rb:69:in `block in <module:Finisher>'
from /usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/initializable.rb:30:in `instance_exec'
from /usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/initializable.rb:30:in `run'
from /usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/initializable.rb:55:in `block in run_initializers'
from /usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:226:in `block in tsort_each'
from /usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:348:in `block (2 levels) in each_strongly_connected_component'
from /usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:429:in `each_strongly_connected_component_from'
from /usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:347:in `block in each_strongly_connected_component'
from /usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:345:in `each'
from /usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:345:in `call'
from /usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:345:in `each_strongly_connected_component'
from /usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:224:in `tsort_each'
from /usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:203:in `tsort_each'
from /usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/initializable.rb:54:in `run_initializers'
from /usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/application.rb:352:in `initialize!'
from /home/ubuntu/workspace/sample_app/config/environment.rb:5:in `<top (required)>'
from /home/ubuntu/workspace/sample_app/config.ru:3:in `require'
from /home/ubuntu/workspace/sample_app/config.ru:3:in `block in <main>'
from /usr/local/rvm/gems/ruby-2.2.1/gems/rack-1.6.4/lib/rack/builder.rb:55:in `instance_eval'
from /usr/local/rvm/gems/ruby-2.2.1/gems/rack-1.6.4/lib/rack/builder.rb:55:in `initialize'
from /home/ubuntu/workspace/sample_app/config.ru:in `new'
from /home/ubuntu/workspace/sample_app/config.ru:in `<main>'
from /usr/local/rvm/gems/ruby-2.2.1/gems/rack-1.6.4/lib/rack/builder.rb:49:in `eval'
from /usr/local/rvm/gems/ruby-2.2.1/gems/rack-1.6.4/lib/rack/builder.rb:49:in `new_from_string'
from /usr/local/rvm/gems/ruby-2.2.1/gems/rack-1.6.4/lib/rack/builder.rb:40:in `parse_file'
from /usr/local/rvm/gems/ruby-2.2.1/gems/rack-1.6.4/lib/rack/server.rb:299:in `build_app_and_options_from_config'
from /usr/local/rvm/gems/ruby-2.2.1/gems/rack-1.6.4/lib/rack/server.rb:208:in `app'
from /usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/commands/server.rb:61:in `app'
from /usr/local/rvm/gems/ruby-2.2.1/gems/rack-1.6.4/lib/rack/server.rb:336:in `wrapped_app'
from /usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/commands/server.rb:139:in `log_to_stdout'
from /usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/commands/server.rb:78:in `start'
from /usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/commands/commands_tasks.rb:80:in `block in server'
from /usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/commands/commands_tasks.rb:75:in `tap'
from /usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/commands/commands_tasks.rb:75:in `server'
from /usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/commands.rb:17:in `<top (required)>'
from /home/ubuntu/workspace/sample_app/bin/rails:8:in `require'
from /home/ubuntu/workspace/sample_app/bin/rails:8:in `<top (required)>'
from /usr/local/rvm/gems/ruby-2.2.1/gems/spring-1.1.3/lib/spring/client/rails.rb:27:in `load'
from /usr/local/rvm/gems/ruby-2.2.1/gems/spring-1.1.3/lib/spring/client/rails.rb:27:in `call'
from /usr/local/rvm/gems/ruby-2.2.1/gems/spring-1.1.3/lib/spring/client/command.rb:7:in `call'
from /usr/local/rvm/gems/ruby-2.2.1/gems/spring-1.1.3/lib/spring/client.rb:26:in `run'
from /usr/local/rvm/gems/ruby-2.2.1/gems/spring-1.1.3/bin/spring:48:in `<top (required)>'
from /usr/local/rvm/gems/ruby-2.2.1/gems/spring-1.1.3/lib/spring/binstub.rb:11:in `load'
from /usr/local/rvm/gems/ruby-2.2.1/gems/spring-1.1.3/lib/spring/binstub.rb:11:in `<top (required)>'
from /home/ubuntu/workspace/sample_app/bin/spring:13:in `require'
from /home/ubuntu/workspace/sample_app/bin/spring:13:in `<top (required)>'
from bin/rails:3:in `load'
from bin/rails:3:in `<main>'
Missing :controller key on routes definition, please check your routes. (ArgumentError)
如错误所述,您需要指定 controller
和 action
匹配 route
。以下应该有效。
get "help", to: 'static_pages#about', as: 'help'
^^^^^^ ^^^^
controller action
此外,我建议您遵循这些 Guides 以清楚地了解如何定义路由。