前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Rails MVC 和 CRUD(7)

Rails MVC 和 CRUD(7)

作者头像
franket
发布2021-11-24 09:50:03
1880
发布2021-11-24 09:50:03
举报
文章被收录于专栏:技术杂记

指定 /articles 为对应的 URL

代码语言:javascript
复制
[root@h202 blog]# bin/rake routes
Running via Spring preloader in process 13088
      Prefix Verb   URI Pattern                  Controller#Action
    articles GET    /articles(.:format)          articles#index
             POST   /articles(.:format)          articles#create
 new_article GET    /articles/new(.:format)      articles#new
edit_article GET    /articles/:id/edit(.:format) articles#edit
     article GET    /articles/:id(.:format)      articles#show
             PATCH  /articles/:id(.:format)      articles#update
             PUT    /articles/:id(.:format)      articles#update
             DELETE /articles/:id(.:format)      articles#destroy
        root GET    /                            welcome#index
[root@h202 blog]# 

(这里的 /articles 明明对应两个方法,GET、POST ,有点不太明白,为什么这样指定就一定成了POST请求)

随便填写东西,提交

rails12.png
rails12.png
rails13.png
rails13.png

原因是 ArticlesController 中找不到对应的 create 方法


定义 create 方法

代码语言:javascript
复制
[root@h202 blog]# cat app/controllers/articles_controller.rb 
class ArticlesController < ApplicationController
  def new
  end
end
[root@h202 blog]# vim app/controllers/articles_controller.rb 
[root@h202 blog]# cat app/controllers/articles_controller.rb 
class ArticlesController < ApplicationController
  def new
  end
  def create
  end
end
[root@h202 blog]# 

重新提交一次

rails14.png
rails14.png

这回又是找不到模板,不过变成了找不到 create 模板

暂时不尝试去直接解决这个模板问题

我们将刚才获取到的内容直接反馈回来

修改create方法,直接打印获取到的参数

代码语言:javascript
复制
[root@h202 blog]# vim app/controllers/articles_controller.rb 
[root@h202 blog]# cat app/controllers/articles_controller.rb 
class ArticlesController < ApplicationController
  def new
  end
  def create
    render plain: params[:article].inspect
  end
end
[root@h202 blog]#

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 定义 create 方法
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档