我的路线文件与预期结果不符

时间:2018-08-11 11:40:31

标签: ruby-on-rails

在此先感谢您的帮助。

我正在通过迈克尔·哈特尔(Michael Hartl)的“足够学习”的RoR课程进行学习,并且正处于通过会话对用户进行身份验证的阶段。

Per the lesson,我刚刚运行rails generate controller Sessions new,没有炸毁大块:

$ rails generate controller Sessions new
Running via Spring preloader in process 4407
      create  app/controllers/sessions_controller.rb
       route  get 'sessions/new'
      invoke  erb
      create    app/views/sessions
      create    app/views/sessions/new.html.erb
      invoke  test_unit
      create    test/controllers/sessions_controller_test.rb
      invoke  helper
      create    app/helpers/sessions_helper.rb
      invoke    test_unit
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/sessions.coffee
      invoke    scss
      create      app/assets/stylesheets/sessions.scss

但是,本课说的routes.rb应该看起来和实际看起来之间是有差异的。

本课暗示,在route.rb中

预期结果

Rails.application.routes.draw do
  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
end

实际结果在routes.rb中:

Rails.application.routes.draw do
  get 'sessions/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'
  post '/signup',  to: 'users#create'
  resources :users
end

我缺少明显的东西吗?为什么会有所不同?

我觉得这里和最后一章之间缺少一章,它仅解释了浏览器关闭会话和cookie过期会话之间的真正高水平差异。

2 个答案:

答案 0 :(得分:0)

您运行了rails generate controller Sessions new,这意味着您只想使用new操作生成一个控制器。默认情况下,new生成视图路由(GET)和实际资源创建路由(POST)。

要创建控制器并遵循CRUD模式进行路由,应运行rails generate controller Sessions

答案 1 :(得分:0)

已解决。

差异的原因是 private int id; private String title; private String body; public postmodel(int id, String title, String body) { this.id = id; this.title = title; this.body = body; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getBody() { return body; } public void setBody(String body) { this.body = body; } 仅生成提到的GET路由@vnbrs。

本课需要3条需要支持的路由:登录表单的rails generate controller Sessions new请求,创建会话的GET请求和销毁会话的POST请求。会议。

需要手动将这些添加到routes.rb:

DELETE

初学者迷失了方向,我看不见森林覆盖树木,伤了我的脑筋。 (P.S.我不建议在几个月后尝试不间断地解决本教程...您的大脑不会停下来。)