目标
当使用者新增或者编辑某篇文章以后,会回到该篇文章的页面。为了要让投票这个功能更流畅一点,我们希望新增或者编辑文章后,要回到文章列表的页面上
步骤
步骤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.’送出通知信息,并显示在文章列表上面