Rails 链接到显示以外的其他视图

Rails Linking to an alternative view other than show

这是Rails3.2

我有一个访问列表和一个按钮,我想用它来调用除显示之外的其他视图的访问。 haml 中的按钮代码是:

  link_to 'Checkout', render('checkout'),  class: 'btn btn-mini btn-danger'

该按钮位于访问视图中,我在 visits/views

下有 checkout.html.haml

似乎 render 应该这样做,但实际上没有

如何渲染默认显示以外的不同视图

render 我认为应该在控制器操作中调用,我认为您不应该在 haml(视图)代码中使用它。

(1) 你的 haml 代码应该有类似
- 这个 link,点击后会重定向到访问控制器的 checkout 动作。

link_to 'Checkout', checkout_visit_path(visit)

(2) 为了让#1 起作用,您应该将它添加到您的 routes.rb 文件

get '/visits/:id/checkout', to: 'visits#checkout', as: 'checkout_visit'  

或者如果您的 routes.rb

中有资源 blabla
resources :visits do
  member do
    get :checkout
  end
end

(3) 为 visits_controller.rb

中的 checkout 动作编写控制器动作
def checkout
  visit = Visit.find_by_id(params[:id])

  # since we named the controller action as "checkout",  
  # it will look for a file named 'checkout.html' (for html request), or 'checkout.json' (for json request), etc. automatically (and the code block below may not be needed)
  # if for example you want to render another file, do this
  respond_to do |format|
    format.html { render template: 'some/other/file/to/load' }
  end
end