如何重定向/渲染到另一个控制器的视图?

时间:2015-10-31 01:55:26

标签: ruby-on-rails

保存宠物后,我想重定向到对待/显示控制器。我试过这样做,但它让我回到宠物/秀。

      float [ ] triNormals = new float [ indices.length ];

      for ( int i = 0 , index = 0 ; i < indices.length ;  )
      {
         float x , y , z;

         x = vertices [ ( 3 * indices [ i ] ) ];
         y = vertices [ ( 3 * indices [ i ] ) + 1 ];
         z = vertices [ ( 3 * indices [ i ++ ] ) + 2 ];

         Vector3f p1 = new Vector3f ( x , y , z );

         x = vertices [ ( 3 * indices [ i ] ) ];
         y = vertices [ ( 3 * indices [ i ] ) + 1 ];
         z = vertices [ ( 3 * indices [ i ++ ] ) + 2 ];

         Vector3f p2 = new Vector3f ( x , y , z );

         x = vertices [ ( 3 * indices [ i ] ) ];
         y = vertices [ ( 3 * indices [ i ] ) + 1 ];
         z = vertices [ ( 3 * indices [ i ++ ] ) + 2 ];

         Vector3f p3 = new Vector3f ( x , y , z );

         Vector3f u = Vector3f.subtract ( p2 , p1 );
         Vector3f v = Vector3f.subtract ( p3 , p1 );

         Vector3f normal = Vector3f.crossProduct ( u , v );

         triNormals [ index ++ ] = normal.x;
         triNormals [ index ++ ] = normal.y;
         triNormals [ index ++ ] = normal.z;
      }

2 个答案:

答案 0 :(得分:1)

您可以为重定向路径执行此操作:

format.html  { redirect_to(treat_path(@treat) }

答案 1 :(得分:1)

  1. 您的@pets变量应为@pet ...
  2. 像这样:

    def create
       @pet = Pet.new pet_params
    

    -

    <强> 2。您无法定义@treat变量:

    @treat = Treat.find params[:treat_id] ?
    

    这显示在这里:

    {:action=>"show", :controller=>"treat", :id=>nil} 
    

    所以答案是你需要定义@treat。由于我不了解您的关联,或者您希望如何填充@treat,因此有两种方法可以创建它:

    association(IE @pet.treat)中提取或明确调用它:(@treat -)...

    为简单起见,您最好执行以下操作:

    def create
        @pets = Pet.new pet_params 
        @treat = Treat.find [[[num - where do you get this from?]]]
    
        ...
    end
    

    -

    如果您的模型设置如此:

    #app/models/pet.rb
    class Pet < ActiveRecord::Base
       has_many :treats
    end
    
    #app/models/treat.rb
    class Treat < ActiveRecord::Base
       belongs_to :pet
    end
    

    您可以使用以下内容:

    def create
        @pet = Pet.new pet_params
        @treat = @pet.treat.new treat_params
    
        respond_to do ...
    
    end
    
    private
    
    def treat_params
       params.require(:treat).permit(:treat, :params)
    end