2013-03-18 13 views
0

Rails新手在這裏。Rails使用單個部分的所有視圖的CRUD按鈕

我想使用單個部分在所有視圖上創建我的CRUD按鈕,而不管控制器如何。所以這就是我迄今爲止所做的工作。在我的控制,我有:

information_controller.rb

class InformationController < ApplicationController 
    before_filter :set 

    def set 
    #Set Paths 
    @new_path = new_information_path 
    @edit_path = edit_information_path 
    end 

    #Then normal index, show, etc definitions follows 

end 

我將採取指數和編輯網頁作爲一個例子。

index.html.haml

[email protected] = "index" #To let partial know what page it is in 
-render 'form', operation: @operation 

edit.html.haml

[email protected] = "edit" 
- render 'form', operation: @operation 

然後,在我的部分形式,我有:

_form.html。 haml

.form-inputs 
    .container-fluid 
    .span8 
     .simple_form_for @foo do |f| 
     =f.input :title, as: :string 
     =render 'controls', f: f, operation: @operation 

,並在我的控制形式只是用來顯示CRUD按鈕,無論控制器的,我有:

_controls.html.haml

-if(operation=="new") 
    link_to "Create", @new_path, class: "btn btn-success" 
-else 
    -if(operation=="edit") 
    =f.submit "Update" 
    -else 
    .span3 
     %table 
     %tr 
      %td=link_to "Edit", @edit_path(f), class: "btn btn-primary" 
      %td=link_to "Delete", f, confirm: "Are you sure", method: :delete, class: "btn btn-danger"   

因此,這可以很好地用於索引加載'編輯,刪除和創建'按鈕的頁面。但我不知道如何將控制器中的edit_information_path正確分配給@edit_path,因爲這需要編輯參數'f'。

賦值@new_path = new_information_path起作用,但@edit_path需要'f'。任何技巧?

回答

1

試試這個:

link_to "Edit",{:controller => params[:controller], :action => :edit, :id => f.object.id}, class: "btn btn-primary" 

或:

link_to "Edit",{:controller => controller_name, :action => :edit, :id => f.object.id}, class: "btn btn-primary" 
+0

這是正確的答案。謝謝。 – Mabz 2013-03-18 17:59:08

相關問題