2011-05-07 127 views
1

使用RailsAdmin。我有一個Post模型和一個用戶模型。每篇文章都屬於一位用戶。RailsAdmin - 隱藏表單域和標籤

我用下面的代碼來獲取RailsAdmin處理協會爲我和自動設置USER_ID創建帖子時:

config.model Post do 
    edit do 
     field :user_id do 
     # use a form_field instead of the default drop-down 
     partial :form_field 
     # hide the input 
     view_helper :hidden_field 
     # set the value to current_user.id 
     def value 
      bindings[:view]._current_user.id 
     end 
     end 
    end 
    end 

此代碼的工作,它設置USER_ID到了CURRENT_USER和它也從視圖中隱藏了form_field(html輸入),以至於用戶甚至不知道它正在爲它們設置。

雖然有一個小問題。雖然我能夠隱藏form_field,我不能隱藏它相關聯的標籤(即讀入後面會出現一個輸入「用戶」標籤) - 這意味着我的用戶看到這一點:

enter image description here

如您所見,有一個標籤「用戶」,旁邊有一個空白空間,下面是「必需」一詞。

是否有反正隱藏輸入的相關標籤(而不僅僅是輸入本身),以便它不會讓用戶感到困惑?我的代碼有問題嗎?

在此先感謝

回答

1

我想我已經找到了有效的解決方案:

field :user_id do 
    view_helper :hidden_field 

    # I added these next two lines to solve this 
    label "" 
    help "" 

    partial :form_field 
    def value 
     bindings[:view]._current_user.id 
    end 
    end 

不理想,但它的工作原理

2

我也遇到同樣的障礙,經過一番試錯誤(以及來自rails_admin組的提示)到達了輕微的替代方案:

config.model Library do 
    edit do 
    field :user_id do 
     # This hides the field label 
     label :hidden => true 
     # This hides the help field *yuk* 
     help "" 
     def value 
     bindings[:view]._current_user.id 
     end 
     # This hides the field input 
     view_helper do 
     :hidden_field 
     end 
    end 
    end 
    field :name 
end