在ActiveAdmin

时间:2017-08-20 18:57:14

标签: ruby-on-rails ruby activeadmin

我是ruby on rails的新手,并且正在使用ruby on rails第4版进行项目。现在我正在更改活动管理员中的订单视图,但我收到错误。

ActiveAdmin.register Order do
    permit_params :id, :order_completed, :approved

    # changing the visual of view action, so admin user can able to see complete order details
    show do |order|
        attributes_table do
            row :id # Order ID
            row 'Name' params[:user].first_name # its not working, not showing the user first_name assiated with order id 
            # It should be user name. although user first_name is in users table and User model is availble.
            row 'Adress' params[:user].address # its not working, not showing the user address assiated with order id
            # It should be user address. although user first_name is in users table and User model is availble.
        end
    end

    controller do
        def show 
            order_id = Order.find(params[:id]).user_id
            params[:user] = User.find(order_id)
            show!
        end
    end
end

我的目标是显示订单相关用户的详细信息,如地址,姓名等。

订单表包含

 - id
 - user_id
 - order_completed (boolean)
 - approved
 - order_status (string) [in process, done]

用户表格由

组成
 - id
 - first_name
 - last_name
 - address
 - membership_id

1 个答案:

答案 0 :(得分:0)

我会假设你已经建立了班级与班级之间的关系。和班级用户'在他们各自的模型中。 (belongs_to和has_many方法。如果您需要更多帮助,请告诉我。) 然后你必须在注册类时在Activeadmin 中声明这种关系,所以试试这个:

    app/admin/order.rb:
    ActiveAdmin.register Order do
        permit_params :id, :order_completed, :approved
        belongs_to :user # Declaring that order belongs to user here

        # changing the visual of view action, so admin user can able to see complete order details
        show do |order|
            attributes_table do
                row :id # Order ID
                row 'Name' do
                    @order.user.first_name
                end
                row 'Address' do 
                    @order.user.address
                end
            end
        end

        controller do
            def show 
                @order = Order.find(params[:id])
                show!
            end
        end
    end`