简体   繁体   English

Ruby在Rails上的重定向

[英]Redirection in ruby on rails

I am a beginner in ruby on rails. 我是红宝石的初学者。 So the scenario is that I am in an index page and I am performing an update in a form displayed on that page. 因此,情况是我在索引页面中,并且正在以该页面上显示的形式执行更新。 Once the update is done I wish to redirect it back to the index page and reflect the update. 更新完成后,我希望将其重定向回索引页面并反映更新。 i tried this, 我试过了

 def update
    format.html { redirect_to action: "index"}
  end

This does not seem to work. 这似乎不起作用。 Any place I am going wrong? 我哪里出问题了?

It does not work, becuase format is not defined. 它不起作用,因为未定义format Either use 无论使用

def update
  # some logic...
  respond_to do |format|
    format.html { redirect_to action: "index"}
  end
end

or drop respond_to and leave just: 或放下respond_to并离开:

def update
  # some logic...
  redirect_to action: "index"
end

respond_to is a method that lets you define how your controller action will respond to given format, eg: respond_to是一种方法,可让您定义控制器操作如何响应给定格式,例如:

def update
  # some logic...
  respond_to do |format|
    format.html { redirect_to action: "index"}
    format.json { render json: {bleh: :blah} }
  end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM