rails 练习7 -- 建立新文章后导回到文章列表

目标

当使用者新增或者编辑某篇文章以后,会回到该篇文章的页面。为了要让投票这个功能更流畅一点,我们希望新增或者编辑文章后,要回到文章列表的页面上

步骤

步骤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
  end
end

一样档案,找到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.’送出通知信息,并显示在文章列表上面
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值