1、render 和 redirect_to
render :action => 'XXX' #只是寻找"XXX"该页面,并不执行其对应的action.不会向浏览器发送新的请求,仍然可以使用当前请求的参数和变量
redirect_to :action => 'XXX' #即向浏览器发送一个新的请求,跳转到该action
def create @user = User.new(params[:user]) respond_to do |format| if @user.save flash[:notice] = 'User was successfully created.' format.html { redirect_to(@user) } # 跳转到index页面 format.xml { render :xml => @user, :status => :created, :location => @user } else format.html { render :action => "new" } #使用new页面进行渲染 format.xml { render :xml => @user.errors, :status => :unprocessable_entity } end end end
2、 config/routes.rb
如果在 controller AAA中新建了一个action BBB,但 http://localhost:3000/AAA/BBB 却报错 Couldn 't find AAA with ID=BBB
打开config/routes.rb 文件添加url路径 map.connect"AAA/BBB" ,:controller=>'AAA',:action=>‘BBB‘ map.resources :AAA #如果原来已有这句话,一定要放在上一句的后面