2013-01-15 64 views
2

我有一個需要編輯的模型,它與當前用戶名爲BillingProfile相關聯。如何添加鏈接到當前用戶的BillingProfile的編輯頁面的菜單項?我不希望或需要BillingProfile的索引頁面,因爲用戶只能編輯自己的頁面。使用ActiveAdmin編輯單個記錄

class User 
    has_one :billing_profile 
end 

回答

3

您可以使用康康來管理允許用戶編輯自己的賬單配置文件的功能。

ability.rb

... 
cannot :edit_billing_profile, User do |u| 
    u.user_id != user.id 
end 
... 

管理/ users.rb的

ActiveAdmin.register User do 
    action_item :only => :show do 
    link_to "Edit BP", edit_bp_path(user.id) if can? :edit_billing_profile, user 
    end 
end 

或者你可以嘗試這樣的事:

ActiveAdmin.register User do 
    form do |f| 
    f.inputs "User" do 
     f.input :name 
    end 
    f.inputs "Billing Profile" do 
     f.has_one :billing_profile do |bp| 
     w.input :address if can? :edit_billing_profile, bp.user 
     end 
    end 
    f.buttons 
    end 
end 

我沒有測試它,但我沒有項目上類似的東西。

+1

我最終做了類似的事情... –

0

我定義了一個LinkHelper它有以下兩種方法:

#This will return an edit link for the specified object instance 
def edit_path_for_object_instance(object_instance) 
    model_name = object_instance.class.to_s.underscore 
    path = send("edit_#{model_name}_path", object_instance) 
end 

#This will return an show link for the specified object instance 
def show_path_for_object_instance(object_instance) 
    model_name = object_instance.class.to_s.underscore 
    path = send("#{model_name}_path", object_instance) 
end 

您可以從您的視圖直接調用edit_path_for_object_instance方法並傳入user.billing_profile對象。

這會給你直接鏈接到實體產生的URL等/ billing_profile/ID /編輯

一種替代方法是使用fields_for。這將允許您爲用戶屬性創建表單並同時更新關聯的BillingProfile。這將是這個樣子:

<%= form_for @user%> 
    <%= fields_for @user.billing_profile do |billing_profile_fields| %> 
    <%= billing_profile_fields.text_field :name %>  
    <% end %> 
<%end%> 

在這裏看到:http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html

+1

我在ActiveAdmin內工作,雖然...我想弄清楚如何使用他們的DSL來實現類似的東西。 –

1

這可能會幫助你 -

添加自定義鏈接:

ActiveAdmin.register User, :name_space => :example_namespace do 
    controller do 
    private 
    def current_menu 
     item = ActiveAdmin::MenuItem.new :label => "Link Name", :url => 'http://google.com' 
     ActiveAdmin.application.namespaces[:example_namespace].menu.add(item) 
     ActiveAdmin.application.namespaces[:example_namespace].menu 
    end 
    end 
end 

我基本上創建了一個新ActiveAdmin :: MenuItem,並將其添加到當前具有名稱空間example_namespace的ActiveAdmin菜單,並在最後返回菜單current_menu方法。注意:current_menu是ActiveAdmin預期的方法,因此不要更改它的名稱。您可以添加任意數量的項目,並將這些項目中的每一個都轉換爲導航標題上的鏈接。請注意,這適用於ActiveAdmin版本> 0.4.3,因此如果您想爲版本< = 0.4.3執行此操作,您可能需要進行自己的挖掘。

+1

這個問題並沒有真正得到菜單中的鏈接,我認爲它只允許用戶編輯他們自己的賬單資料。建立索引頁面甚至查看結算配置文件是沒有意義的。 –