zoukankan      html  css  js  c++  java
  • rails中render 和 redirect_to的区别, each只能用在数组中,如果只有一个或者零个项,用each方法会报错undefined method `each' for #...

    在render中,即使有:action,那么也仅仅是取对应的view中的模板(html.erb)而已,所以这里即使浏览器中的url是/orders/xcreate,但是显示的界面是/app/views/orders/xshow的代码

      def xcreate
        flash.now[:notice] = "bb"
        @order = Order.new(params[:order])
        if @order.save
    	  flash[:notice] = "yes"
    	  render(:action => 'xshow')
    	else
    	  flash.now[:alert] = "no"
    	end
      end
    

    在redirect_to中,直接重定向到要去的界面,所以在/orders/new中点击提交按钮后,不会到/order/xcreate的url,浏览器中直接跳转到/orders(就是/orders/index)去了。

      def xcreate
        flash.now[:notice] = "bb"
        @order = Order.new(params[:order])
        if @order.save
    	  flash[:notice] = "yes"
    	  redirect_to(:action => 'index')
    	else
    	  flash.now[:alert] = "no"
    	end
      end
    

      

    在index.html.erb中

    <% @order.each do |oo| %>
      <div>
        <%= oo.name %>   <%= link_to 'see', {:controller => "orders",:id => oo, :action => "xshow"} %><br/>
        <%= sanitize(oo.des) %>
      </div>
    <% end %>
    

      

    作为示例,第一个render是定向到xshow,第二个redirect_to是定向到index,

    是因为第一个只有一个实例,但是index中用的是数组,所以如果render index的话,会看见其中报错undefined method `each' for #...  是因为这个不是一个数组。需要修改下代码才行。

  • 相关阅读:
    Oracle学习笔记<5>
    Oracle学习笔记<4>
    fabric动态获取远程目录列表
    fabric查看本地与远程主机信息
    fabric安装使用
    pexpect实现远程操作
    pexpect的pxssh类实现远程操作
    Most Distant Point from the Sea
    Art Gallery
    Rotating Scoreboard(半平面交模板题)
  • 原文地址:https://www.cnblogs.com/juandx/p/3964524.html
Copyright © 2011-2022 走看看