Rails路由:指向同一视图的两个操作

时间:2013-12-10 16:49:28

标签: ruby-on-rails rails-routing

我正在尝试创建两个都进入“新”视图的操作。唯一的区别是我希望new_e_drawing操作运行incrament_e方法,而新操作运行incrament方法。

  def new
    @drawing = Drawing.new
    @drawing = @drawing.incrament(@drawing)

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @drawing }
    end
  end

  def new_e_drawing
    @drawing = Drawing.new
    @drawing = @drawing.incrament_e(@drawing)

    respond_to do |format|
      format.html  new.html.erb
      format.json { render json: @drawing }
    end
  end

我希望他们俩都带我到名为“新”的视图。我不确定如何为new_e_drawing操作设置路由或respond_to语句。我尝试了这些没有成功:

 get 'drawings/new' => 'drawings#new_e_drawing'
 match 'drawings/new_e_drawing' => 'drawings#new_e_drawing'

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

在new_e_drawing操作的html块中显式渲染“new”模板。

 def new
    @drawing = Drawing.new
    @drawing = @drawing.incrament(@drawing)

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @drawing }
    end
  end

  def new_e_drawing
    @drawing = Drawing.new
    @drawing = @drawing.incrament_e(@drawing)

    respond_to do |format|
      format.html { render :template => "new" }
      format.json { render json: @drawing }
    end
  end

在你的路线中,

match 'drawings/new_e_drawing' => 'drawings#new_e_drawing'

新操作的路由将由rails自动生成,因为它是CRUD的一部分