BillingProfile という現在のユーザーに関連付けられている編集が必要なモデルがあります。現在のユーザーの BillingProfile の編集ページにリンクするメニュー項目を追加するにはどうすればよいですか? ユーザーは自分のページしか編集できないため、BillingProfile のインデックス ページは必要ありません。
class User
has_one :billing_profile
end
BillingProfile という現在のユーザーに関連付けられている編集が必要なモデルがあります。現在のユーザーの BillingProfile の編集ページにリンクするメニュー項目を追加するにはどうすればよいですか? ユーザーは自分のページしか編集できないため、BillingProfile のインデックス ページは必要ありません。
class User
has_one :billing_profile
end
Cancan を使用して、ユーザーが自分の課金プロファイルを編集できるようにする機能を管理できます。
アビリティ.rb
...
cannot :edit_billing_profile, User do |u|
u.user_id != user.id
end
...
admin/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
私はそれをテストしていませんが、プロジェクトで同様のことをしました。
これはあなたを助けるかもしれません-
カスタム リンクの追加:
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で実行する場合は、独自の掘り下げが必要になる場合があることに注意してください。
次の 2 つのメソッドを持つ 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 オブジェクトを渡すことができます。
これにより、エンティティへの直接リンクが提供され、/billing_profile/ID/edit のような URL になります。
もう 1 つの方法は、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