rails 练习7 -- 建立新文章后导回到文章列表
来源:互联网 发布:微信淘宝优惠券怎么做 编辑:程序博客网 时间:2024/06/06 03:46
目标
当使用者新增或者编辑某篇文章以后,会回到该篇文章的页面。为了要让投票这个功能更流畅一点,我们希望新增或者编辑文章后,要回到文章列表的页面上
步骤
步骤1:修改topics controller
打开app/controllers/topics_controller.rb,并且找到create这个action
找到这行:
format.html { redirect_to @topic, notice: 'Topic was successfully created.' }
将@topic改成topics_path如下面示例:
format.html { redirect_to topics_path, notice: 'Topic was successfully created.' }
整个create action代码如下
def create @topic = Topic.new(topic_params) respond_to do |format| if @topic.save format.html { redirect_to topics_path, notice: 'Topic was successfully created.' } format.json { render :show, status: :created, location: @topic } else format.html { render :new } format.json { render json: @topic.errors, status: :unprocessable_entity } end endend
一样档案,找到update这个action
找到这行:
format.html { redirect_to @topic,notice: 'Topic was successfully updated.' }
将@topic改成topics_path如下面示例:
format.html { redirect_to topics_path,notice: 'Topic was successfully updated.' }
步骤2:确认修改成功
前往http://localhost:3000操作看看!
解说
format.html { redirect_to topics_path,notice: ‘Topic was successfully created.’ }:
- format.html代表网站会回传HTML内容回去给浏览器
- redirect_to topics_path代表完成create或update后导向文章列表页面
- notice: ‘Topic was successfully created/updated.’送出通知信息,并显示在文章列表上面
0 0
- rails 练习7 -- 建立新文章后导回到文章列表
- rails 练习9 --整理文章列表上的连接
- rails 练习4 --对文章做投票
- Blogger建立新文章 - Blog透视镜
- 我的新博客地址以及文章列表
- rails 练习8 --将文章标题变成连接
- 文章列表
- 文章列表
- 文章列表
- 文章列表
- 文章列表
- 文章列表
- 文章列表
- 新文章
- 新文章
- 新文章
- 新文章
- 新文章
- 生成二维码
- mysql复习小结一
- JavaScript闭包-闭包的作用域
- 使用DjangoUeditor将Ueditor移植到Django(BAE环境下)
- 获取项目根的路径的方法
- rails 练习7 -- 建立新文章后导回到文章列表
- shell if/date/echo换行/数组遍历
- mybatis处理集合、循环、数组和in查询等语句的使用
- 字符串统计
- iOS 多媒体(一)音频播放
- 后缀数组(一)——hiho120最长可重叠重复K次子串
- AndroidStudio快捷键
- ecshop二次开发--左侧导航
- Rxjava(创建类)--from